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

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

Asked by 6 years ago
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

Log in to vote
0
Answered by 6 years ago

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.

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! alfredo_linguini 24 — 6y
0
No problem, also, it was because I moved the entire character model and not a part inside the character like what SuperMario did :) Operation_Meme 890 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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)

Answer this question