SA-MP Forums Archive
Get alive players - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Get alive players (/showthread.php?tid=527917)



Get alive players - babecka - 25.07.2014

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?


Re: Get alive players - Stinged - 25.07.2014

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;
}



Re: Get alive players - Ihateyou - 25.07.2014

if health != 0


Re: Get alive players - babecka - 25.07.2014

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.


Re: Get alive players - Konstantinos - 25.07.2014

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.


Re: Get alive players - Ihateyou - 25.07.2014

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


Re: Get alive players - Ihateyou - 25.07.2014

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


Re: Get alive players - icra - 25.07.2014

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.


Re: Get alive players - babecka - 25.07.2014

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