Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this click-to-teleport to part script move everyone?

function onClicked()

local c = game.Players:GetChildren() 
for i = 1, #c do 
c[i].Character.UpperTorso.CFrame = CFrame.new(script.Parent.Parent.L8.Position)

end 

script.Parent.ClickDetector.MouseClick:connect(onClicked)

L8 is a part name in my game. When one player clicks on the teleport pad it teleports everyone ingame to L8 (the part) when we only want the person who clicks it to teleport. It's a two-way teleporting system and the parts are contained within a model.

2 answers

✓ Accepted answer

While what SuperMario9595 said was correct, I will go more into detail as to why it is happening.

In your for loop, it is going through all the children of Players individually, so it will teleport all the players in the game to that location. You can fix this by finding the single player that has clicked on the clickdetector by following SuperMario9595's answer.

script.Parent.ClickDetector.MouseClick:connect(function(player)
	player.Character:MoveTo(script.Parent.Parent.L8.Position)
end)

The above should work.

2 comments

0

SuperMario's script is what ended up working for me (since your script made welded parts fall off of players, which we need for our game) but thank you for the thorough explanation!

0

No problem, also, it was because I moved the entire character model and not a part inside the character like what SuperMario did :)

MouseClick has a way to know what player clicked it, so you can use that.

local function onClicked(playerWhoClicked)
	playerWhoClicked.Character.UpperTorso.CFrame = CFrame.new(script.Parent.Parent.L8.Position)
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

This discussion is preserved as it was. New answers, comments, and votes are closed.