Get # of players online?
#1

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

Login in rcon and do /rcon players
Reply
#3

but in a script?
Reply
#4

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;
}
Reply
#5

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.
Reply
#6

@ 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 :/
Reply
#7

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);
Reply
#8

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);
Reply
#9

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.
Reply
#10

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?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)