# of alive players

Hey guys, just wondering if anyone knows how to get the number of how many alive players there are, can’t find it anywhere.

[lua]
function getNumAlivePlayers()
local count = 0;

for k, v in pairs( player.GetAll() ) do
	if( v:Alive() && v:Health() > 0 )then
		count = count + 1;
	end
end

return count;

end

print( getNumAlivePlayers() );
[/lua]

^ This, except with

player.GetHumans instead (player.GetAll() gets bots as well)

[editline]19th April 2016[/editline]

Also, I don’t think you need to check if the player’s health is above 0- you can have a health of 0 and still be alive

Could just do not ply:Alive() instead of checking the health.

(also stop using && and ; please, it looks bad)

It looks bad but it’s preference, sadly you can’t change that.

Also if anything to note on the code he should’ve used ipair, no idea how often this function is gonna be called but its good practice either way.

There’s no different using AND or &&, in-fact I think && and || look much better then AND OR. I don’t personally use ;, but no reason why not to, in-fact it keeps you in good habit for other languages.

“keeps you in good habit for other languages”, switching between using them isn’t hard so I don’t see the reason you’d need to keep it as a habit.

Also the main point brought up about it is that &&, ||, and the other variations of them are specific to Gmod Lua, if you were to ever use Lua somewhere else you’d need to use the default operators. That and if you ever wanted to use something you made for Gmod outside of Gmod you’d have to convert all the operators.

Why woukd someone use something they created for gmod outaide of it?

Because not everything made in gmod is specific to gmod.

It’s used in C

You can create many libraries in Gmod and still be able to use them outside of Gmod, I wasn’t specifically talking about things using Gmod functions.

Arguing about “;”.
Why.
Please just stop.
Please.

&& and || are used in other languages, they’re not GLua specific. Ye my ‘good habit’ comment was kinda dumb. I was just being a dick. I just meant to stop calling people out on he way they code. It’s up to the individual person. As long as it works, it fine. Your way of coding isn’t the best, neither is mine, neither is his. Obviously you do get badly written code and well written code, but not because of weather or not they use && or || and ;.

In the context of lua, yes, it is specific only to glua.

Oh, apologies. I thought we were on about other languages other then Lua. In that case yes - and now the ‘use outside of gmod’ comment makes more sense :stuck_out_tongue: But still, I use &&/|| because 1. I think it looks neater and stands out from the other ‘words’ in coding, but also because I code in PHP etc. so used to using it to.

I am tempted to write a plugin that randomly shuffles all known syntaxas in a file. Just cause the “Use x”,“x it looks better” or “x is pure lua”. :v

That does !sound like something that you || anyone else would use.
You and I && everyone else reading this is already set in their ways, or has an opinion one way || the other.;
&& besides, there are more important things to work on and fix.;