Trouble with getting the number of players in a server

I want to achieve a script that has a variable that is the number of players in the server.

The issue is that if I print the variable, it just says 0.
I have tried switching between GetChildren() and GetPlayers().

This is the code:

local players = game:GetService("Players")
local count = #players:GetPlayers()

if count < 2 then
    print("Not enough players to start the game.")
    print(count)	
end		

Also this is my first topic so if I need to change anything just say.

for _, v in ipairs(game.Players:GetChildren()) do
	print(v) -- print everything inside the players service
end

for _, v in ipairs(game.Players:GetPlayers()) do
	print(v) -- will only print if a player object found
end
1 Like

Dumb question. Is your scripti firing this once a game starts? if so. Itll print 0 because all clients hasnt loaded yet

repeat task.wait()
until game:IsLoaded()

No it fires every time a new game starts

task.wait(5)
local players = game:GetService("Players")
local count = players:GetPlayers()

if #count < 2 then
    print("Not enough players to start the game.")
    print(#count)	
else
print("Enough players to start the game!")
print(#count)
end

Not sure if this will help you. But you can try. It waits .1 seconds until the “:GetPlayers()” is higer or equal to the “Required” value :slight_smile:

local Required = 1
local players = game:GetService("Players")

repeat task.wait(.1) until #players:GetPlayers() >= Required


local count = #players:GetPlayers()

if count < 2 then
	print("Not enough players to start the game.")
	print(count)	
end		

Oh, thankyou! That’s even better for what I was aiming for.

1 Like