How to get the count of players?
#1

Sorry, I've been frequently asking few questions lately here. How to get the count of players? I have 6 gangs on my own script, and I made a command /gangs to get the count of the gang members in each gang. For example, "Grove Street Families members = 9 members". I would like to know how to count the members in each gang and write the names of the gang members in the list. Write an example for me and use your own desired gang variables and I will change it to mine variables.

The help will get +rep, because you really deserve it! Thanks in advance.
Reply
#2

This uses foreach, if you don't have it already, download it! It returns the gang the players are in.
pawn Код:
#define MAX_GANGS 9

new bool:pGangs[MAX_PLAYERS][MAX_GANGS];

CMD:gangs(playerid, cmd[], cmdtext[])
{
    foreach(new i: Player)
    {
        for(new g = 0; g < MAX_GANGS; g ++)
        {
            if(pGangs[i][g] == true)
            {
                format(string, sizeof(string), "Gang %d: %s (%d)", g, PlayerName(i), i);
                SendClientMessage(playerid, -1, string);
            }
        }
    }
    return 1;
}

stock PlayerName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    return name;
}
This returns the players in a specific gang!
pawn Код:
#define MAX_GANGS 2

new bool:pGangs[MAX_PLAYERS][MAX_GANGS];

CMD:grove(playerid, cmd[], cmdtext[])
{
    foreach(new i: Player)
    {
        if(pGangs[i][0] == true)
        {
            format(string, sizeof(string), "Gang %d: %s (%d)", 0, PlayerName(i), i);
            SendClientMessage(playerid, -1, string);
        }
    }
    return 1;
}

CMD:ballas(playerid, cmd[], cmdtext[])
{
    foreach(new i: Player)
    {
        if(pGangs[i][1] == true)
        {
            format(string, sizeof(string), "Gang %d: %s (%d)", 1, PlayerName(i), i);
            SendClientMessage(playerid, -1, string);
        }
    }
    return 1;
}

stock PlayerName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    return name;
}
This uses sscanf, download it if you don't have it! This returns the members in a gang defined by the user!
pawn Код:
#define MAX_GANGS 2

new bool:pGangs[MAX_PLAYERS][MAX_GANGS];

CMD:gangmembers(playerid, cmd[], cmdtext[])
{
    new gangid;
    if(sscanf(cmdtext, "i", gangid)) return SendClientMessage(playerid, -1, "Usage: /gangmembers (gang id).");
    if(gangid < 0 || gangid > MAX_GANGS) return SendClientMessage(playerid, -1, "You have entered an invalid gang id.");
    foreach(new i: Player)
    {
        if(pGangs[i][gangid] == true)
        {
            format(string, sizeof(string), "Gang %d: %s (%d)", gangid, PlayerName(i), i);
            SendClientMessage(playerid, -1, string);
        }
    }
    return 1;
}

stock PlayerName(playerid)
{
    new name[MAX_PLAYER_NAME];
    GetPlayerName(playerid, name, MAX_PLAYER_NAME);
    return name;
}
Reply
#3

Thank you. +repped!
Reply
#4

I'm using DIALOG_STYLE_LIST, so when I did this:

pawn Код:
if(response) // If they clicked 'Select' or double-clicked a weap
                {
                            switch(listitem)
                            {
                                foreach(new i: Player)
                                {
                                    case 0: if(PlayerInfo[i][pGang] = 2000), format(string, sizeof(string), "%s (%d)", PlayerName(i), i), ShowPlayerDialog(playerid, DIALOG_GROVES, DIALOG_STYLE_LIST, "Gang: Grove Street Families", string1, "Close", "");
These errors I get:

pawn Код:
GM.pwn(1410) : error 002: only a single statement (or expression) can follow each "case"
GM.pwn(1410) : error 029: invalid expression, assumed zero
How could I make that code in a single statement to follow "case"? Is that even possible?
Reply
#5

You should use foreach loop out of the switch or in each case.
Reply
#6

Please, can you show me an example? I wrote the line error up there.
Reply
#7

I did that:
pawn Код:
switch(listitem)
                            {
                                    case 0: foreach(new i: Player), if(PlayerInfo[i][pGang] = 2000), format(string, sizeof(string), "%s (%d)", PlayerName(i), i), ShowPlayerDialog(playerid, DIALOG_GROVES, DIALOG_STYLE_LIST, "Gang: Grove Street Families", string1, "Close", "");
                                    case 1: foreach(new i: Player), if(PlayerInfo[i][pGang] = 2001), format(string, sizeof(string), "%s (%d)", PlayerName(i), i), ShowPlayerDialog(playerid, DIALOG_BALLAS, DIALOG_STYLE_LIST, "Gang: Ballas", string1, "Close", "");
And I got these errors
pawn Код:
GM.pwn(1410) : error 001: expected token: ";", but found "if"
GM.pwn(1410) : error 017: undefined symbol "i"
Reply
#8

he means this:
pawn Код:
if(response) // If they clicked 'Select' or double-clicked a weap
         {
                    switch(listitem)
                    {
                                case 0:
                                {

                                        foreach(new i: Player)
                                        {
                                             if(PlayerInfo[i][pGang] = 2000), format(string, sizeof(string), "%s (%d)",        PlayerName(i), i), ShowPlayerDialog(playerid, DIALOG_GROVES, DIALOG_STYLE_LIST, "Gang: Grove Street Families", string1, "Close", "");
                                        }
                               }
                      }
           }
           return 1;
Reply
#9

pawn Код:
if(response) // If they clicked 'Select' or double-clicked a weap
{
switch(listitem)
{
case 0:
{
foreach(new i:Player)
{
if(PlayerInfo[i][pGang] = 2000), format(string, sizeof(string), "%s (%d)", PlayerName(i), i), ShowPlayerDialog(playerid, DIALOG_GROVES, DIALOG_STYLE_LIST, "Gang: Grove Street Families", string1, "Close", "");
}
}
}
}
Reply
#10

I've done what you said:
pawn Код:
if(response)
{
    switch(listitem)
    {
        case 0:
        {
            foreach(new i: Player)
            {
                if(PlayerInfo[i][pGang] = 2000),
                {
                    format(string, sizeof(string), "%s (%d)",  PlayerName(i), i), ShowPlayerDialog(playerid, DIALOG_GROVES, DIALOG_STYLE_LIST, "Gang: Grove Street Families", string1, "Close", "");
                }
            }
         }
     }
 }
Once again, I got errors:

pawn Код:
GM.pwn(1449) : error 014: invalid statement; not in switch
GM.pwn(1449) : warning 215: expression has no effect
GM.pwn(1449) : error 001: expected token: ";", but found ":"
GM.pwn(1449) : error 029: invalid expression, assumed zero
Line 1449 is:
pawn Код:
case 4:
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)