SA-MP Forums Archive
Get # of players online? - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Get # of players online? (/showthread.php?tid=241816)



Get # of players online? - sciman001 - 18.03.2011

How do i get the number of players online? eg. new players = GetAllPlayersOnline(); or something... please help me!


Respuesta: Get # of players online? - Alex_Obando - 18.03.2011

Login in rcon and do /rcon players


Re: Get # of players online? - sciman001 - 18.03.2011

but in a script?


Re: Get # of players online? - Calgon - 18.03.2011

There's no SA-MP function to count how many players are online, but you can easily create one.

pawn Код:
stock CountPlayersOnline() {
    new
        iCount;
    for(new i = 0; i < MAX_PLAYERS; i++) {
        if(IsPlayerConnected(i)) iCount++;
    }
   
    return iCount;
}



Re: Get # of players online? - Biesmen - 18.03.2011

Top of your script, below the defines:
pawn Код:
new players;
OnPlayerConnect:
pawn Код:
players++;
OnPlayerDisconnect:
pawn Код:
players--;
Somewhere in your script:
pawn Код:
stock GetAllPlayersOnline()
{
  return players;
}
Usage:
pawn Код:
GetAllPlayersOnline()
@ Post above: Why would you use loops for such an easy and small thing.


Re: Get # of players online? - Jochemd - 18.03.2011

@ Calg00ne: I think you can better do when player connects: PlayersOnline++ and when one leaves: PlayersOnline-- than looping through all players every time (however it's better if you only use it once).

And someone was faster than me :/


Re: Get # of players online? - sciman001 - 18.03.2011

Quote:
Originally Posted by Biesmen
Посмотреть сообщение
Top of your script, below the defines:
pawn Код:
new players;
OnPlayerConnect:
pawn Код:
players++;
OnPlayerDisconnect:
pawn Код:
players--;
Somewhere in your script:
pawn Код:
stock GetAllPlayersOnline()
{
  return players;
}
Usage:
pawn Код:
GetAllPlayersOnline()
@ Post above: Why would you use loops for such an easy and small thing.
so if i wanted to print the players online in the server thing... i would do printf("players online: %s", players);


Re: Get # of players online? - Biesmen - 18.03.2011

Quote:
Originally Posted by sciman001
Посмотреть сообщение
so if i wanted to print the players online in the server thing... i would do printf("players online: %s", players);
Almost, the code is correct, except the placeholder. %s is for strings, you should use %i, or %d.

So:
pawn Код:
printf("Players Online: %d", players);



Re: Get # of players online? - Calgon - 18.03.2011

Quote:
Originally Posted by Jochemd
Посмотреть сообщение
@ Calg00ne: I think you can better do when player connects: PlayersOnline++ and when one leaves: PlayersOnline-- than looping through all players every time (however it's better if you only use it once).

And someone was faster than me :/
Yes, I know that, but the fact is that if he's using it in an include without hooking, then he's going to evidently have a lot of trouble.


Re: Get # of players online? - sciman001 - 19.03.2011

Quote:
Originally Posted by Calg00ne
Посмотреть сообщение
There's no SA-MP function to count how many players are online, but you can easily create one.

pawn Код:
stock CountPlayersOnline() {
    new
        iCount;
    for(new i = 0; i < MAX_PLAYERS; i++) {
        if(IsPlayerConnected(i)) iCount++;
    }
   
    return iCount;
}
ok... so.. if i wanted to use this, i would write CountPlayersOnline(); and then printf("players online: %i", iCount); in a timer that has an interval of 10000 or something?