I was working on my game, but I came across a roadblock. I wanted to use an if statement to detect if an object was anchored, and if it is, then the player teleports. I have tried to get this to work for hours with no luck. Any advice?
You can check if the part's anchored property is true and then teleport the player by setting their HumanoidRootPart to the part. To do this, you could do the following:
local part = workspace.Part
local plr = game.Players.LocalPlayer or game.Players.LocalPlayer:CharacterAdded:Wait()
-- wait for character to fully load in
if part.Anchored then
plr.Character.HumanoidRootPart.Position = part.Position -- teleport the player to the part
end
Related
I'm a newbie developer of roblox.
I'm trying to place a player in a specific position on first load in this way:
In StarterPlayer > StarterPlayerScripts I added a LocalScript with the following code:
local cf = CFrame.new(500, 5, 50)
local Char = game.Players.LocalPlayer
Char.HumanoidRootPart.CFrame = cf
When I click play, nothing happen onload.
What I'm doing wrong?
#ItsJustFunboy is half correct, you do need to use the Model:MoveTo function, but you might have some other issues as well : timing and LocalScripts.
Timing-wise, you have this code in StarterPlayerScripts. These LocalScripts execute when the Player joins the game, not when their character model appears. So it's possible that their character doesn't even exist at the time you're telling it to move. If you want to guarantee that the character model exists at the time you run this code, it needs to be in StarterCharacterScripts.
As for the problem with LocalScripts, they make changes to the world only on your client. Meaning that if you move your character in a LocalScript, then all the other players will still see your character model where it was, not where you moved it. So unless this is intentional, if you want everyone to see this change, you need to move the character model in a server-side Script.
So in a Script in the Workspace or in ServerScriptService, try this :
-- wait for a player to join the game
game.Players.PlayerAdded:Connect(function(player)
-- wait for their character to load into the game
player.CharacterAdded:Connect(function(character)
-- yield the thread for but a moment so that the character can finish loading
wait()
-- move their character
local targetPosition = Vector3.new(500, 5, 50)
character:MoveTo(targetPosition)
end)
end)
use the MoveTo() function:
local plr = game.Players.LocalPlayer
plr.Character:MoveTo(CFrame.new(500, 5, 50))
I have a key card door but it don't work when I put it in replicatesStorage (it is a gamepass key) can somebody help me it only work when put in starterPack it is currently being given with a localScript at StartGui ** here is code:
script.Parent.Touched:Connect(function(hit)
if hit.Parent.Name == "Clearance1" then
script.Parent.CanCollide = false
script.Parent.Transparency = 0.5
wait(0.5)
script.Parent.CanCollide = true
script.Parent.Transparency = 0
end
end)
StarterPack is already replicated, in a way.
If you use ReplicatedStorage, you'll have to add it to PlayerInstance.Backpack.
Also, script.Parent.Touched is server-side only.
If you wanna access the player's backpack, you can use game.Players.PLAYERNAME.Backpack
The way you're doing this is actually not a very good idea. (no offense)
I would recommend leaving the item inside the StarterPack. If you really don't want to, you can programmatically do it by putting it in their Backpack. Like this:
-- Server script
game.Players.PlayerAdded:Connect(function(player) -- Runs when a player joins the game
player.CharacterAdded:Connect(function() -- Runs when that player respawns or their character loads
local itemCopy = game.ReplicatedStorage.Clearance1:Clone() -- Creates a copy of the item
itemCopy.Parent = player.Backpack -- Puts the item in the player's backpack (inventory)
end)
end)
What that code does is: Every time the player spawns, it clones the item and puts it in their inventory.
Now to check if the user has the item when they touch the door, you can do something like this:
-- Server script
script.Parent.Touched:Connect(function(part) -- Activates when a part touches the doorf
local player = game.Players:GetPlayerFromCharacter(part.Parent) -- Gets a player from the part that touched
if player and player.Backpack:FindFirstChild("Clearance1") then -- Makes sure there is a player, and the player has the keycard in their inventory
script.Parent.CanCollide = false -- Makes the part uncollidable
script.Parent.Transparency = 0.5 -- Sets the part to have transparency
wait(0.5) -- Waits half a second
script.Parent.CanCollide = true -- Makes the part collidable
script.Parent.Transparency = 0 -- Makes the part not transparent
end
end)
Every time the part is touched, it checks if it's a player. If it is, it checks if the player has the item. If so, it runs the code.
The button, which I'm trying to use, is in StarterGUI. Don't know if it helps. Fairly new to scripting, however saw another post with something similar and used that code. Here is my code inside the LocalScript:
local MarketplaceService = game:GetService("MarketplaceService")
local player = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.PurchaseEvent
local productID = 1218445531
script.Parent.MouseButton1Click:Connect(function()
PurchaseEvent:FireServer(productID)
end)
if MarketplaceService:PlayerOwnsAsset(1218445531) then
--Here is where the thing is supposed to happen
end
First you will need to get a reference to the part, one way is to use something like local part = workspace:WaitForChild("PartName")
In order to make it visible, you need to set the transparency to 0. If you dont want the player walking through the part you may also need to turn collisions on with CanColide. Anchoring the part is also recommend to prevent it from falling.
part.Transparency = 0; -- makes it visible
part.CanColide = true; -- makes sure other objects cant go through this
part.Anchored = true; -- prevents the object from falling
There are many ways to make a part move. If the part is supposed to go from one place to another, and only has one destination, a Tween might be useful (see TweenService).
Another way would be with a Heartbeat loop that changes the position each frame (see RunService.Heartbeat). For example,
local keepMoving = true; -- change this to false to stop moving
local RunService = game:GetService("RunService");
local direction = Vector3.new(0, 1, 0); -- change this to anything you want
while (keepMoving) do
local timePassed = RunService.Heartbeat:Wait(); -- wait one frame, roughly 0.016 of a second.
part.Position += direction * timePassed; -- move the part in the direction
end
This would move part up 1 stud every second.
I can't Get my Gamepass Working Because of this. here is my code:
local TENROBUXGUI = script.Parent
local TextButton = TENROBUXGUI.TextButton
TextButton.MouseButton1Up:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit)
if player then
game:GetService("MarketplaceService"):PromptGamePassPurchase(player, 21187565)
end
end)
You seem to be using hit in the same way that you would find a BasePart that touches another part in the game world using the Touched event. However, as this is a GUI, it doesn't work the same way.
According to documentation, MouseButton1Up has two number parameters that correspond to the exact x and y coordinates where the user clicks/taps on their screen. Since the first parameter corresponds to that x coordinate value and not the Model of a player in the game world, it returns null.
So instead, what you're looking to do is to refer to game.Players.LocalPlayer to get the Player object of the player that clicked the GUI.
local TENROBUXGUI = script.Parent
local TextButton = TENROBUXGUI.TextButton
TextButton.MouseButton1Up:Connect(function()
local player = game.Players.LocalPlayer
if player then
game:GetService("MarketplaceService"):PromptGamePassPurchase(player, 21187565)
end
end)
In Roblox I want to create.If the player has a Tool in backpack and touches brick.Give Player 2000
cash,and tool dispears.I already have made a leaderboard for the cash,and already have my tool in server storoge.
First off, you can start by using a .Touched event which fires every time an object is touched. A .Touched event can carry one parameter; the object that touched it. For example, if a player (R15) stepped on a block on the floor, the object that hit the part might be Left Foot. Here is what the skeleton of a .Touched function looks like:
<Part>.Touched:Connect(function(hit)
print(hit.Name) --As per the example above, this would print "Left Foot"
end)
Since any object can touch the part, but not every one has a Backpack, you'll want to first check if the object that hit your part is from an actual player. You can do this by looking for a Humanoid, which would be a sibling of the part that touched. You can do this by adding:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
print(hit.Name) --As per the example above, this would print "Left Foot"
end
end)
Now that we have verified that a player was what touched our part, we can use the function of game.Players called GetPlayerFromCharacter(). A Character is just your player in game.workspace, so by calling this function, it gets your player in game.Players from your character. This is similar to game.Players:FindFirstChild(<Player>.Name). Either one work, but I will be using the first one for this example. Added to your code, here is what it should now look like:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
end
end)
As you may know, Backpack is a direct child of the player in game.Players, so we can use player.Backpack to find our Backpack. Now we can do the same thing that we did to check for a humanoid, to check for our tool:
<Part>.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local Backpack = player.Backpack
if Backpack:FindFirstChild("<Tool Name>") then
player.leaderstats.<Money>.Value += 2000 --Gives them 2000 cash
end
end
end)
If you cloned the tool into the player's Backpack previously, you can do Backpack.<Tool Name>:Destroy() to get rid of it/make it dissapear.