Check number of players that are spawned

Note that I want to check all loaded in/spawned players. What I want to do is have a “waiting for players” phase in my gamemode, so I’d need to check the amount of players. The only problem is, the only way I know to do that is with Player:GetCount() and that only works with gamemodes derived from sandbox, mine is derived from base. I was thinking of using the k,v in pairs player.GetAll thing, but I still don’t know how to check how many there are.

Hooray for you! I had the same issue. Use:
[lua]
#player.GetAll()
[/lua]
Want an if statment?
[lua]
if #player.GetAll() >= 1 then
[/lua]

Just a question, what does the number sign/pound sign actually do for player.GetAll()? I may want to use it on some other thing some time, depending on what it does.

It returns the length of the table it’s in front, since player.GetAll is a function that returns a table of all players, you can then ask # (number of) players in the table.

[editline]29th September 2014[/editline]

IIRC it doesn’t work on tables that have non numerical keys or missing numerical keys, but I’ve been out of Lua for a while so I don’t know for sure.

It works in a weird way where it can skip non numerical keys. But yes, best case scenario for an accurate representation of how many entries are in a table is to have all of the keys as consecutive numbers.

It also works on strings.

In GLua or Lua?

IIRC it was only in Glua?

Edit: Just tested, it works in vanilla Lua too.

if you’d like to count a table with non-numerical or non-linear keys, you’d count like this:



local count = 0
for idx in pairs( table ) do
    count = count + 1
end


where count would be equal to the mumber of entries in a given table.
the function table.Count works like this.

[editline]29th September 2014[/editline]

As far as I’m aware, it works standard Lua also