I tried to make this script to teleport a player when they click it, but instead it teleports all the player to the "click2" location. I'm a beginner script. If you can help me solve this it would be great.
function onClicked() local c = game.Players:GetChildren() -- for i = 1, #c do c[i].Character.Torso.CFrame = CFrame.new(script.Parent.Parent.Click2.Position) + Vector3.new(0, 0, 0) end end script.Parent.ClickDetector.MouseClick:connect(onClicked)
local CD = script.Parent:WaitForChild("ClickDetector") -- CD = Click Detector local Tele = script.Parent.Parent:WaitForChild("Click2") function onClicked(player) local char = player.Character or player.CharacterAdded:wait() local torso = char:WaitForChild("Torso") torso.CFrame = CFrame.new(Tele.Position +Vector3.new(0, 3, 0)) end CD.MouseClick:connect(onClicked)
Okay so first we changed up the code quite a bit so I added a cd variable(click detector variable) and a variable for where we want to teleport them (tele variable) so the script is more efficient and I also took you function a changed it to have 1 argument and that's the player that clicked, so it will auto put the player in so no need for you to do so, next we wait for their character to not be nil(wait for their character to exist) then we wait for the torso(cause so things don't load right away and its good practice to use :WaitForChild()) then we teleport them using CFrame.new() and we add 3 studs so they are perfectly on the ground, 3 studs is where the torso is on the character.