Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

is there a problem with the script I wrote?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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)

1 answer

Log in to vote
0
Answered by 9 years ago
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.

0
thank you sir! awesomegamers200 72 — 9y
Ad

Answer this question