How do I get number of players in game?

Hi, I am trying to get number of players for a game, but it just prints out as zero when I try it. Look at the following script:

local players = game:GetService("Players"):GetChildren()
print(#players)

It just prints out as zero. And this is part of a webhook so I can know how many people are in a server at a certain time.

I think it’s 0 because the script is running before any player is there. So you can try this.

local Players = game:GetService("Players")
local amount = 0

function changed()
    amount = #Players:GetPlayers()

    print(amount)
end

Players.PlayerAdded:Connect(changed)
Players.PlayerRemoving:Connect(changed)
2 Likes

Please do this:

local Players = game:GetService("Players")

--On whatever event you call 
function GetPlayers()
      return #Players:GetPlayers()
end
1 Like

This should be what you need:

local Players = game:GetService("Players")
local numPlayers = #Players:GetPlayers()

print(numPlayers) --// Should return 0 since the script runs before any players are in the game

As well as the first post and the post above

1 Like

@TwoMadDev @AmoraFolf @calvin_coco
Thanks :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.