for i, player in pairs(game.Players:GetChildren()) do player.Character.Torso.CFrame = ToLobby + Vector3.new(0, i * 5, 0) end
This script is supposed to teleport all the players to ToLobby and stack them on top of each other. This usually works except for when somebody dies as soon as this script is run...
When they die, their torso is removed from the game until respawned, and if someone's dead while it's running that part of the script, it breaks and stops the entire script. Can someone help me fix this so it doesn't do that?
To use their Torso to teleport them, first make sure that their Torso exists.
for i,player in pairs(game.Players:GetPlayers())do local torso=player.Character:FindFirstChild("Torso") if torso then torso.CFrame=ToLobby+Vector3.new(0,i*5,0) end end
An alternative is to use MoveTo:
for _,player in pairs(game.Players:GetPlayers())do player.Character:MoveTo(ToLobby) end
MoveTo will automatically stack the characters.
There is actually a function Roblox created, I use this alot, called MoveTo
, this is ment for teleporting characters, so lets remodel this script to not error. Although modifying the torso's CFrame works, he also may get stuck inside the floor or something so you must do something like this. It seems you already planned for the player getting stuck with with multiplying i by 5. You also need to check if the player.Character exists, or available. Below shows a fixed script demonstrating what I just said.
for i, player in pairs(game.Players:GetChildren()) do if player.Character then player.Character:MoveTo(ToLobby) -- the MoveTo function automatically stacks players, as Roblox already has accounted for most of the teleportation checks. end end