How to check online players
#1

Hello, i have a question how to check how much members are online from group/faction ?
I some to explain me.
I want when players types /mon it will says ex : 4 members online .
Thanks in advance.
Reply
#2

Make a variable to count a faction's online players and loop through all connected players when a player types /mon and check if they're in that faction, if they're raise the variable's value by one, after finishing the loop, format and send the player the desired message.
Reply
#3

pawn Код:
if(strcmp("/mon", cmdtext, true) == 0)
   {
    if(PlayerInfo[playerid][playerteam]<1 || PlayerInfo[playerid][playerteam]==CIV || PlayerInfo[playerid][playerteam]==255) return SendClientError(playerid,"You cant use this Command");
           SendClientMessage(playerid,COLOR_LIGHTYELLOW,"------[MEMBERS ONLINE]------");
           for(new i; i != MAX_PLAYERS; i++)
           {
                if(PlayerInfo[playerid][playerteam] == PlayerInfo[playerid][playerteam])
                {
                    format(string,sizeof(string),"(RANK:%s )( %s[ID:%d]) ",PlayerInfo[playerid][RankName],PlayerName(playerid),i);
                    SendClientMessage(playerid,COLOR_WHITE,string);
                }
            }
           return 1;
       }
i have /mon cmd which shows the online members with their names but i want to count them and displays them as Number .
Reply
#4

Let's say the variable which saves player's faction is PlayerFaction and you want to check for faction ID 8. You gotta create a command as you wish and use that format;
pawn Код:
new count, string[128];
for(new id = 0; id < MAX_PLAYERS; id++)
{
     if(PlayerFaction[id] == 8)
     {
          count++;
     }
}
format(string, sizeof(string), "%d members are online from faction 8.", count);
SendClientMessage(playerid, -1, string);
I hope you understood it.
Reply
#5

pawn Код:
new faction = 0, online[128];

if(strcmp("/mon", cmdtext, true) == 0)
{
    if(PlayerInfo[playerid][playerteam]<1 || PlayerInfo[playerid][playerteam]==CIV || PlayerInfo[playerid][playerteam]==255) return SendClientError(playerid,"You cant use this Command");
    SendClientMessage(playerid,COLOR_LIGHTYELLOW,"------[MEMBERS ONLINE]------");
    for(new i; i != MAX_PLAYERS; i++)
    {
        if(PlayerInfo[playerid][playerteam] == PlayerInfo[playerid][playerteam])
        {
            faction++;
        }
    }
    format(online, sizeof(online), "%d online members!", faction);
    SendClientMessage(playerid, 0xFFFFFFFF, online);
    return 1;
 }
Put the new faction = 0 and online [128]; and the top of your script.
Reply
#6

Everytime when i invite myself in different faction its adding +150 members ..
i get this mesage
150 members online etc
Reply
#7

Use this code.
pawn Код:
if(strcmp("/mon", cmdtext, true) == 0)
{
    new members, online[128];
    if(PlayerInfo[playerid][playerteam]<1 || PlayerInfo[playerid][playerteam]==CIV || PlayerInfo[playerid][playerteam]==255) return SendClientError(playerid,"You cant use this Command");
    SendClientMessage(playerid,COLOR_LIGHTYELLOW,"------[MEMBERS ONLINE]------");
    for(new i; i != MAX_PLAYERS; i++)
    {
        if(PlayerInfo[playerid][playerteam] == PlayerInfo[playerid][playerteam])
        {
            members++;
        }
    }
    format(online, sizeof(online), "%d online members!", members);
    SendClientMessage(playerid, 0xFFFFFFFF, online);
    return 1;
 }
Reply
#8

It still not works.. its only showining - 150 Members online...
Reply
#9

Ryder is comparing playerid with playerid, use this.
pawn Код:
if(strcmp("/mon", cmdtext, true) == 0)
{
    new members, online[128];
    if(PlayerInfo[playerid][playerteam]<1 || PlayerInfo[playerid][playerteam]==CIV || PlayerInfo[playerid][playerteam]==255) return SendClientError(playerid,"You cant use this Command");
    SendClientMessage(playerid,COLOR_LIGHTYELLOW,"------[MEMBERS ONLINE]------");
    for(new i; i < MAX_PLAYERS; i++)
    {
        if(PlayerInfo[playerid][playerteam] == PlayerInfo[i][playerteam])
        {
            members++;
        }
    }
    format(online, sizeof(online), "%d online members!", members);
    SendClientMessage(playerid, 0xFFFFFFFF, online);
    return 1;
 }
Reply
#10

A better alternative would be to have a variable count how many players are in a team/faction when they join the team/faction. For example when you put a player in the cops team increase the "number_of_cops" var by one. When they log off reduce it by one. Then you always have access to the number of players in a team without looping through all players.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)