Checking how much players are currently online
#1

How to check the amount of players online? I want to make my command work when it's higher than an x number of players.
Reply
#2

pawn Код:
stock GetOnlinePlayers()
{
    new amount;
    foreach(Player, i)
    {
        //if(IsPlayerConnected(i))
        amount++;
    }
    return amount;
}
This wasn't tested, though I assume it would work how you want.
I can't remember if foreach checks if the ploayer is connected manually itself. If not, just uncomment that line.

Example of use;
pawn Код:
if(GetOnlinePlayers() < 20) return SendClientMessage(playerid, -1, "There are not enough players online to do this!");
Reply
#3

error 017: undefined symbol "foreach"
Reply
#4

You need foreach include.
Download it here: http://pastebin.com/Seseuh2x
(click download just above the code, and rename the file to "foreach.inc", place it in your pawno > include folder then #include <foreach> at teh top of your script)

Or alternatively, use this;

pawn Код:
stock GetOnlinePlayers()
{
    new amount;
    for(new i; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
            amount++;
    }
    return amount;
}
Reply
#5

I tried downloading foreach, but got many loose indentation warnings.

The alternative works, thanks.
Reply
#6

Quote:
Originally Posted by Moron
Посмотреть сообщение
I tried downloading foreach, but got many loose indentation warnings.

The alternative works, thanks.
Download foreach from the post on this forum, ofcourse pastebin will give loose indentations.
Reply
#7

I took the link of the pastebin from foreach thread. Oh well.
Didn't know about Iter_Count, nice.
Reply
#8

Come on guys, don't assume he's using foreach, use plain PAWN code, granted using foreach is better but not all new scripters know about libraries yet.

pawn Код:
stock GetOnlinePlayers()
{
    new count;
    for(new i; i != GetMaxPlayers(); ++i)
    {
        if(!IsPlayerConnected(i)) continue;
        count++;
    }
    return count;
}
Reply
#9

Quote:
Originally Posted by ******
Посмотреть сообщение
Surely that's a good reason for telling them?
Yes, it's a perfectly good reason to tell them, that doesn't answer his original question. But, I suppose I shall explain to him what it is.

Basically, a library is another script what different people have created to add more functionality to your gamemode / filterscript. This is not just something to do with PAWN, most programming languages have libraries. But I am specifically talking about SA:MP here. Some of them are 'Plug & Play' where all you have to do is include it into your gamemode and it will work straight away, this is achieved by using methods such as ALS. And others add a set of new functions and callbacks, take a look at YSI, it's basically a whole set of libraries which adds many more useful features. People tend to call them includes, but they're basically the same thing. A whole bunch of them can be found here: http://forum.sa-mp.com/forumdisplay.php?f=83

Probably didn't explain that as well as I could of, but it was rushed so meh. And there was really no point in me writing this if you already knew :P, but I guess other people can use it as an explanation.
Reply
#10

Maybe a better way than looping.

pawn Код:
new gConnectedPlayers = 0;
public OnPlayerConnect(playerid)
{
    ++gConnectedPlayers;
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    --gConnectedPlayers;
    return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)