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

Problem with teleporting players?

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

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.

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