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

Help With Teleportation Script?

Asked by
Scootakip 299 Moderation Voter
8 years ago
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

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

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.

0
In the case of using MoveTo, ToLobby must be a Vector3 and not a CFrame. 1waffle1 2908 — 8y
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. Programmix 285 — 8y
Ad
Log in to vote
1
Answered by
Azmidium 388 Moderation Voter
8 years ago

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

Answer this question