How to get all players instead only 1

hi i was making a minigame but i cant teleport all player but only 1 player that gets teleported
here is the script
local players = game.Players:GetChildren()
for i = 1,#players do
if players[i].Character ~= nil then
local spawnLocation = math.random(1,#workspace.Teleports:GetChildren())
players[i].Character:MoveTo(workspace.Teleports:GetChildren()[spawnLocation].Position)
end
end

Why don’t you just do this:

for _, v in pairs(game.Players:GetChildren()) do
  local spawnLocation = math.random(1,#workspace.Teleports:GetChildren())
  v.Character:MoveTo(workspace.Teleports:GetChildren()[spawnLocation].Position)
end

But how do you teleport it to the same teleport part

Oh you want all the players go to the same spawn point?

You can do this then:

local spawnLocation = math.random(1,#workspace.Teleports:GetChildren())
for _, v in pairs(game.Players:GetChildren()) do
  v.Character:MoveTo(workspace.Teleports:GetChildren()[spawnLocation].Position)
end
1 Like

Tysm!! It helped a lott :heart:

@PerilousPanther’s solution works. But you shouldn’t use :GetChildren() when getting the players. It could return something non-player that was parented inside. Thats why game.Players:GetPlayers() exists. So I reccomend updating the code to:

local spawnLocation = math.random(1,#workspace.Teleports:GetChildren())
for _, v in pairs(game.Players:GetPlayers()) do
  v.Character:MoveTo(workspace.Teleports:GetChildren()[spawnLocation].Position)
end

also using :SetPrimaryPartCFrame() i think is better than :MoveTo() as it might move the player onto the top of the building/spawn area sometimes.

1 Like

How would it be better to set people on the roof? CFrame doesn’t care about collisions.

It really depends on what you’re going for and how much work you want. :MoveTo() takes in Roblox’s physics and collisions into consideration while :SetPrimaryPartCFrame() doesn’t. I usually use :MoveTo() whenever there is a risk of the player’s hitbox getting stuck. It really depends on your preferences.