I am scripting a very simple spawning logic as shown below. What I have in mind is: a) create a table, b) get all the players and add them to the table, c) teleport each player to a respective spawnpoint’s cframe.
After running some tests, I have noticed that only the first player who joins the server gets teleported, while all the other players are disregarded. I’ve tried using ipairs in both for loops instead of pairs but it doesn’t help my case. Does anyone know what exactly I’m missing?
local plrs = {}
for i, player in pairs (game.Players:GetPlayers()) do
if player then
table.insert(plrs,player)
end
end
task.wait(1)
local SpawnPoints = map:WaitForChild("SpawnPoints") --finding the folder in the map
if not SpawnPoints then
print("SPAWNPOINTS NOT FOUND") --folder not found
end
local availableSpawnPoints = SpawnPoints:GetChildren() --get the children of folder
--preparing for the spawning of players
lobbyTrack1.Playing = false
--Spawn players at the spawnpoints
for i, player in pairs(plrs) do
if player then
character = player.Character or player.CharacterAdded:Wait()
-- Check if character has a HumanoidRootPart, move hrp to the spawn point
local hrp = player.Character:WaitForChild("HumanoidRootPart")
if hrp and #availableSpawnPoints > 0 then
hrp.CFrame = availableSpawnPoints[1].CFrame + Vector3.new(0, 10, 0)
table.remove(availableSpawnPoints, 1)
local InGame = Instance.New("BoolValue")
InGame.Name = "InGame"
InGame.Parent = player.Character
end
else
if not player then
table.remove(plrs, i)
end
end
end