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

Help With Teleportation Script?

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?

2 answers

✓ Accepted answer

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.

2 comments

0

In the case of using MoveTo, ToLobby must be a Vector3 and not a CFrame.

0

Going to post this again; don't remove my comment this time? It's better to CFrame the HumanoidRootPart than the Torso; the HumanoidRootPart is more reliable in a sense.

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

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