garrysmod-requests icon indicating copy to clipboard operation
garrysmod-requests copied to clipboard

Add a function to retrieve the total player count of a server

Open FredyH opened this issue 9 years ago • 10 comments

Currently the only (non hacky) way to get the player count is using #player.GetAll(). This way only returns the number of players who are already visible to lua. Unfortunately this excludes all players that are still loading in, such as downloading content or loading the gamemode. I suggest adding a (serverside only?) function like game.PlayerCount() that returns the real player count of a server (the same one that is reported by stats or status)

FredyH avatar Nov 01 '16 05:11 FredyH

Something like gmsv_gatekeeper would be nice.

local clients = gatekeeper.GetNumClients() print(clients.active) -- prints the number of clients currently active (have spawned) in the server print(clients.spawning) -- prints the number of clients in the connection process print(clients.total) -- prints the total number of clients in the server (connecting AND in game)

Remixful avatar Nov 01 '16 08:11 Remixful

player.GetCount() has been added

fruitwasp avatar Nov 01 '16 08:11 fruitwasp

@fruitwasp It doesn't do what this request wants.

robotboy655 avatar Nov 01 '16 08:11 robotboy655

But that function could be easily expanded to do that, similarly to what GetClientCount does in SourceMod

neico avatar Nov 01 '16 10:11 neico

I cannot believe such a function doesn't already exist, ridiculous.

MrLewis avatar Jan 02 '17 16:01 MrLewis

This is just a simple binding lol, 4 years for nothing.

Kefta avatar Jun 01 '20 17:06 Kefta

Frustrating that this still doesn't exist, not even a boolean to check if the server is full at the very least

GlorifiedPig avatar Mar 06 '22 15:03 GlorifiedPig

Bump - this is still an issue in 2025. player_connect or NetworkIDValidated don't fire on level changes either so we can't it that way when we need to access connecting players on a map change.

I get that they're technically already validated and connected, but the lack of ability to retrieve them is a bit of a bummer.

Just give us a function with some data (name, steamid, ownerid, stuff we'd already know). Even just for players that are already authenticated would be nice.

xCynDev avatar Mar 19 '25 01:03 xCynDev

This seems like it could be useful even if it's just a count rather than their Steam IDs.

player.GetCount() returns the number of players (including bots) currently on the server, but it excludes connecting players, as it's the same as counting the length of the player.GetAll() table.

I'd suggest a game.CountPlayers() which returns the number of players, including connecting players, and bots. This would match game.MaxPlayers() which returns the maximum number of players (including bots) the server can have.

Ready players = player.GetCount() eg 5 Connecting players = game.CountPlayers() - player.GetCount() eg 7 - 5 = 2 Available slots: = game.MaxPlayers() - game.CountPlayers() eg 10 - 7 = 3

DarthTealc avatar Mar 19 '25 06:03 DarthTealc

This would probably require https://github.com/Facepunch/garrysmod-requests/issues/2417 as IServer::GetNumClients would return the accurate player count.

RaphaelIT7 avatar Mar 21 '25 13:03 RaphaelIT7

local playerCount = 0
hook.Add( "ClientSignOnStateChanged", "playerCountTracker", function( _, oldstate )
    if oldstate ~= 7 then return end
    playerCount= playerCount+ 1
end )

gameevent.Listen "player_connect"
hook.Add( "player_connect", "playerCountTracker", function()
    playerCount= playerCount+ 1
end )

gameevent.Listen "player_disconnect"
hook.Add( "player_disconnect", "playerCountTracker", function()
    playerCount= playerCount- 1
end )

A way to do it in lua right now using hooks

wrefgtzweve avatar May 31 '25 13:05 wrefgtzweve

Added player.GetCountConnecting - returns only a count of players who are connecting, but not yet spawned in.

robotboy655 avatar Jun 04 '25 16:06 robotboy655