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

Hey! If its a party system then you have to send the players in the current party/lobby/round… whatever, to the other server via using TeleportOptions! Then from there you can check if they all loaded it… But some may not, some will haha.

PS: Sorry for the VERY late response

player joins and dynamically build the players table

local Players = game:GetService("Players")
local players = {}

Players.PlayerAdded:Connect(function(player)
    table.insert(players, player)
end)

repeat
    task.wait(1)
until #players == 5 --not sure what you mean by it may not be the same

Can see a few problem with this however… Think dduck5tar solution would be a bit less error-prone.
A player may quit the game before 5 are set up…

This is something I learned recently, but if you use the service, the service yeilds until the character is loaded.

-- Connect the function to the Players.PlayerAdded event
game.Players.PlayerAdded:Connect(function(player)
  -- Wait for the player's character to load
  player.CharacterAdded:Connect(function()   
    --logic here
  end)
end)

What this does is it yeilds the rest of it until that character is added, and when it is things SHOULD be fine. Note things can happen, some people have long load times and are on iphone8’s or something so… even though this is a “solution” you should go deeper into solving it. This is where I think the other guy Pepsi_cat is right, BindableEvents are king.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)   
		local humanoid=character:WaitForChild("Humanoid")
		--
	end)
end)

I like to do this. There is a slight stall between the character loaded and the description being added to it. humanoid being used here for that stall. Description added takes around 0.03

1 Like

Yeah of course, but as long as you use the service! It will yeild until the whole character is added, meaning if you run the logic after you are good so yes this will work too.

Not always… I have ran into problems with this and now use this. That’s how I know the time it takes.
My player has many layers of items and an animated face … but so might others.
There is a command called CharacterAppearanceLoaded:Wait() but it doesn’t always work.

1 Like

Well you do understand studio, sometimes I load my character way before my server loads. Weird things happen in studio especially if you load “too fast” and the other way around. I have had it to where I load into studio and my character didn’t load. But this method is what I have seen on the dev documentation. But either way, it will work. TO be extra safe you can even wait or do task.delay() that is the safest option.

1 Like

It could be harder to maintain and update multiple places. Check TeleportData/TeleportOptions in the roblox documentation. I personally send over the amount of expected players during the teleport.