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

Problem with teleporting players?

Asked by
R_alatch 394 Moderation Voter
8 years ago

Error: 1 is not a valid member of Part

for i, player in pairs(game.Players:GetPlayers()) do
    player.Character.Humanoid.WalkSpeed = 0

    for i, spawns in pairs(MapClone:FindFirstChild("Spawns"):GetChildren()) do
        player.Character:MoveTo(spawns[i].Position)
    end
end

1 answer

Log in to vote
2
Answered by 8 years ago

I can see 2 problems with your script. First, you are trying to reference objects named '1', '2', '3', etc. I doubt that you named your spawns that way, based on your error.

You should use the pairs function correctly, like this:

for i, spawns in pairs(MapClone:FindFirstChild("Spawns"):GetChildren()) do
    player.Character:MoveTo(spawns.Position)
end

Second, you are teleporting every player to every spawn, making all of them finally teleport to the last spawn. To fix this, you should make a table variable that holds the spawns' children and then reference that table.

Here is how to do that:

local spawns = MapClone:FindFirstChild("Spawns"):GetChildren()

for i, player in pairs(game.Players:GetPlayers()) do
    player.Character.Humanoid.WalkSpeed = 0
    player.Character:MoveTo(spawns[i].Position)
end

You just have to make sure that number of spawns is greater than or equal to maximal player count or this code will error.

Hope this helped.

Ad

Answer this question