Filter players, and get count

How can I get the amount of players, for example, have a certain health, have a custom variable set to a certain number, or model, etc?

Loop through all the players and check them for a certain condition. If they match, insert them into a second table. Use table.Count or the length operator (#) on the new table to get the number of players that match.

Here are some examples of things you could check for:

[lua]
function PlayersWithLowHealth() --Checks if players have low health. If they do, it adds them to the table.
local tbl = {}
for _,v in pairs(player.GetAll()) do
if v:Health() <= 50 then
table.insert(tbl,v)
end
end
print(table.Count(tbl))
end

function PlayersWithModel() --Checks if players have the alyx model. If they do, it adds them to the table.
local tbl = {}
for _,v in pairs(player.GetAll()) do
if v:GetModel() == “models/player/alyx.mdl” then
table.insert(tbl,v)
end
end
print(table.Count(tbl))
end

function PlayersWithVariable() --Checks if players have a custom variable set to 72. If they do, it adds them to the table.
local tbl = {}
for _,v in pairs(player.GetAll()) do
if v.Variable == 72 then
table.insert(tbl,v)
end
end
print(table.Count(tbl))
end
[/lua]

Thank you.