I am working on a game, and I am trying to teleport all the players to different spawn points, but it is not working. I have tried many different solutions but none are working, here is what I have:
for i, v in pairs(game.Players:GetPlayers()) do
local spawns = random:FindFirstChild("Spawns")
local points = spawns:GetChildren()
v.Character:WaitForChild("HumanoidRootPart").CFrame = points[1].CFrame
table.remove(points,1)
end
The random before the :FindFirstChild is a random map, the “Spawns” is a model containing all of the spawn points,
Everyone is teleported to the same spot, how can I fix this?
I think you forgot to put the i in the points[].CFrame. When its points[1].CFrame, it means that it is directing to that one specific spawn point which is why all the players are teleporting to that one same spawn point.
for i, v in pairs(game.Players:GetPlayers()) do
local spawns = random:FindFirstChild("Spawns")
local points = spawns:GetChildren()
v.Character:WaitForChild("HumanoidRootPart").CFrame = points[i].CFrame --This
table.remove(points,1)
end
You’re re-declaring the “points” array every iteration of the loop, so table.remove(points, 1) isn’t going to affect future iterations of the same loop. Because of this the same spawns will end up being reused for multiple players, you need to declare the array outside of the loop instead, like this.
local spawns = random:FindFirstChild("Spawns")
local points = spawns:GetChildren()
for i, v in pairs(game.Players:GetPlayers()) do
v.Character:WaitForChild("HumanoidRootPart").CFrame = points[i].CFrame --This
table.remove(points,1)
end