so im trying to use the MoveTo function to teleport the player who touched it. But im not sure where to go from here to make it activate when touched. Heres what i have so far.
game.Players.PlayerAdded:connect(function(p) p.CharacterAdded:connect(function(character) p.Character:MoveTo(Vector3.new(214,20,119)) end) end)
so would i use .Touched somewhere inside that? another function? or something else?
Your question is a bit unclear but I'll try to help you.
Workspace.yourBrick.Touched:connect(function(p) if(game.Players:GetPlayerFromCharacter(p.Parent) == nil) then return end -- This means that the part that touched this part isn't apart of a character. p.Parent:MoveTo(Vector3.new(214,20,119)) end)
Let's begin.
This fires when any brick touches the brick this event is attached to.
Quite simple. This moves the whole model to a point in the world based on the first parameter.
Scrap your script. There's no point in using the .PlayerAdded
or the .CharacterAdded
events. They're redundant.
What you should be focusing on is the .Touched event.
-- If this script is in the teleport part itself TeleportBrick = script.Parent function onTouched(hit) -- "hit" is the object that touched the teleport part -- Your code here end TeleportBrick.Touched:connect(onTouched) -- This is a connection line. It establishes a "listener" to call the function.
You should add some safeguards to ensure that the model that's touching the teleport part is the character of the Player instance itself, such as the :GetPlayerFromCharacter() and the :FindFirstChild() methods.
:GetPlayerFromCharacter()
: Verifies if the character is connected to a player. If true, it will return the player instance. If false, it will return nil.
:FindFirstChild()
: Verifies if the object exists, utilizing the string of the name. If true, it will return the object. If false, it will return nil.
-- In your onTouched() function local HumanoidIsFound = hit.Parent and hit.Parent:FindFirstChild("Humanoid") local PlayerIsFound = hit.Parent and game.Players:GetPlayerFromCharacter if HumanoidIsFound and PlayerIsFound then -- If all of the prerequisites are good, then... hit.Parent:MoveTo(214, 20, 119) -- Teleport this character to here! end