Get alive players
#1

Hello. I'm trying to get the amount of players alive, however, it is not working for me.
I've tried using a variable which would increase when a player spawned and decrease when a player died, however, for some reason it increased 2 times for 1 player.

I've also tried using a stock
Код:
stock IsPlayerAlive()
{
	for (new i = 0; i < MAX_PLAYERS; i ++)
	{
	    if(GetPlayerHealth(i) == 100.0) return true;
	}
	return false;
}
but when I checked the amount of player alive like this
Код:
if(IsPlayerAlive() == 1)
I've got a warning: number of arguments doesn't match definition and it didn't work.

How can I check it, please?
Reply
#2

I didn't test it, but try this:
pawn Код:
stock GetPlayersAlive()
{
    new count = 0;
    new Float:hp;
    for (new i = 0; i < MAX_PLAYERS; i++)
    {
        GetPlayerHealth(i, hp);
        if (hp > 0) count++;
    }
    return count;
}
Reply
#3

if health != 0
Reply
#4

Quote:
Originally Posted by Stinged
Посмотреть сообщение
I didn't test it, but try this:
pawn Код:
stock GetPlayersAlive()
{
    new count = 0;
    new Float:hp;
    for (new i = 0; i < MAX_PLAYERS; i++)
    {
        GetPlayerHealth(i, hp);
        if (hp > 0) count++;
    }
    return count;
}
Doesn't work.
Reply
#5

If a player is alive, their state will be either on foot or in vehicle (driver or passenger).

pawn Код:
new
    count;

foreach(new i : Player)
{
    switch (GetPlayerState(i))
    {
        case 1 .. 3: ++count;
    }
}
PS: foreach is much faster for player loops.
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
If a player is alive, their state will be either on foot or in vehicle (driver or passenger).

pawn Код:
new
    count;

foreach(new i : Player)
{
    switch (GetPlayerState(i))
    {
        case 1 .. 3: ++count;
    }
}
PS: foreach is much faster for player loops.
nice u gona make him download includes just for one loop lol gg
Reply
#7

Quote:
Originally Posted by Ralfie
Посмотреть сообщение
I feel sorry for you.
no need to feel sorry for me .. i know more then u kid
Reply
#8

Your code error is there:
Код:
GetPlayerHealth(i) == 100.0)
Function GetPlayerHealth doesn't returns Player's Health, but it stores Player's Health in a variable.
You should create a Floated variable, store player's health, and verify if player's health is equals to 100.

Anyway, Player may have less than 100 HP but he can be still alive, so i suggest you to check if his HP are more than 0.

Third thing, your function will check if there are or not alive players. I suggest you doing a function which checks if a single player is alive or not, then trough a loop get the count and check if count result is = 0 or > 0.

Here's an example:

Код:
IsPlayerAlive(playerid) {
  new Float:HP;
  GetPlayerHealth(playerid, HP);

  if(HP > 0) {
   return true; // Player is alive
  } else {
    return false; // Player isn't alive
  }
}

CountAlivePlayers() {
  new count;

  for(new i = 0; i < MAX_PLAYERS; i++) {
    if(IsPlayerAlive(i) && IsPlayerConnected(i)) {
      count += 1;
    }
  }
  return count;
}
So, if you want to check if there are or not alive players, just:

Код:
if(CountAlivePlayers() > 0) {
  return true; // There are alive players
} else {
  return false; // There aren't alive players
}
Hope i helped you.
Reply
#9

Thank everyone (but not Ihateyou) for the help! The problem has been fixed when I used icra's code, thank you!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)