How to wait until every player is loaded

So i have this script:

local getPlayers = game.Players:GetPlayers()

local players = {

	player1 = getPlayers[1],
	player2 = getPlayers[2],
	player3 = getPlayers[3],
	player4 = getPlayers[4]
}

It takes all the players and assigns them a variable because I need it to do that

The issue, is that it comes out as nil because the player’s dont load in time

I have found this:

local Game = game
local Players = Game:GetService("Players")

repeat
	Players.PlayerAdded:Wait()
until #Players:GetPlayers() == 5 --Wait for 5 players.

it waits until all the players are loaded, but the issue is that you have to put in a number of how many players it should wait for, but that’s not always gonna be the same in the game

5 Likes

Hey @Drixitty, This script is an Simpler version of the script you found and it uses the PlayerAdded event of the Players service to detect when a player is added to the game then it waits for the player’s character to be loaded using the CharacterAdded event.

local Players = game:GetService("Players")

local function onPlayerAdded(player)
    local character = player.Character or player.CharacterAdded:Wait()
    -- Your code here
end

Players.PlayerAdded:Connect(onPlayerAdded)

Hope this Helps!

this would be my take on this

local PlayersWithoutCharacters = {}
local EveryoneLoadedEvent = Instance.new("BindableEvent")
local plrs = game:GetService("Players")
local FirstPlayerLoaded = false

plrs.PlayerAdded:Connect(function(plr)
table.insert(PlayersWithoutCharacters,plr)
plr.CharacterAdded:Connect(function()
index = table.index(PlayersWithoutCharacters,plr)
if index then
table.remove(PlayersWithoutCharacters,plr)
end
end)
FirstPlayerLoaded = true
end)
local WaitingFunction = game:GetService("RunService").Heartbeat:Connect(function()
if FirstPlayerLoaded then
if #PlayersWithoutCharacters < 1 then
EveryoneLoadedEvent:Fire()
WaitingFunction:Disconnect()
end
end
end)

not sure if this would work but try it

on line 12 it says attempted to call nil value

its this line:

index = table.index(PlayersWithoutCharacters,plr)

hi it’s still pringting nill

(post must be 30 letters so i have to type this)

change table.index to table.find, i forgot the syntax

on this line:

	if index then
		table.remove(PlayersWithoutCharacters,plr)
	end

this is the error message

ServerScriptService.PlrOneDobas:13: invalid argument #2 to ‘remove’ (number expected, got Instance)

1 Like

wrong syntax again. change plr to index

This might not be possible if your game does not use a logging system before a game cycle starts because people can join a server at any time. You will probably need a voting system with a timer that logs which players are going to join the game cycle. After the timer runs out you can close the voting system and be sure of which players are loaded and available for the game cycle to begin.

If this is not possible you may have to choose a minimum player count, i.e.:

until #Players:GetPlayers() >= 5 -- or whatever min you choose

yeah i think what i might do is im gonna have lobbies anyway so just get the player count from there and set that as the value that it has to wait for