CMD problem
#1

So i made a /admins cmd and its shows me this following errors i am using foreach and i dont know whats the issue

here is my code
pawn Код:
CMD:admins(playerid, params[])
{
    new aMsg[MAX_PLAYER_NAME];
    SendClientMessage(playerid, COLOR_RED, "Administrators Online");
    foreach(Player, i)
    if(pInfo[i][Adminlevel] == 1 && 2)
    {
        format(aMsg, 127, "Moderator: %s", GetName(i));
        SendClientMessage(playerid, COLOR_PINK, aMsg);
    }
    if(pInfo[i][Adminlevel] == 3)
    {
        format(aMsg, 127, "Adminisator: %s", GetName(i));
        SendClientMessage(playerid, COLOR_PINK, aMsg);
    }
    if(pInfo[i][Adminlevel] == 4)
    {
        format(aMsg, 127, "Manager: %s", GetName(i));
        SendClientMessage(playerid, COLOR_PINK, aMsg);
    }
    if(pInfo[i][Adminlevel] == 5)
    {
        format(aMsg, 127, "Owner: %s", GetName(i));
        SendClientMessage(playerid, COLOR_PINK, aMsg);
    }
    return 1;
}
Errors
Код:
TDMv1.0.pwn(998) : error 017: undefined symbol "i"
TDMv1.0.pwn(1000) : error 017: undefined symbol "i"
TDMv1.0.pwn(1003) : error 017: undefined symbol "i"
TDMv1.0.pwn(1005) : error 017: undefined symbol "i"
TDMv1.0.pwn(1008) : error 017: undefined symbol "i"
TDMv1.0.pwn(1010) : error 017: undefined symbol "i"
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase
Reply
#2

Foreach still opens like a normal for loop.

pawn Код:
CMD:admins(playerid, params[])
{
    new aMsg[MAX_PLAYER_NAME];
    SendClientMessage(playerid, COLOR_RED, "Administrators Online");
    foreach(Player, i)
    {
        if(pInfo[i][Adminlevel] == 1 && 2)
        {
            format(aMsg, 127, "Moderator: %s", GetName(i));
            SendClientMessage(playerid, COLOR_PINK, aMsg);
        }
        if(pInfo[i][Adminlevel] == 3)
        {
            format(aMsg, 127, "Adminisator: %s", GetName(i));
            SendClientMessage(playerid, COLOR_PINK, aMsg);
            continue;
        }
        if(pInfo[i][Adminlevel] == 4)
        {
            format(aMsg, 127, "Manager: %s", GetName(i));
            SendClientMessage(playerid, COLOR_PINK, aMsg);
            continue;
        }
        if(pInfo[i][Adminlevel] == 5)
        {
            format(aMsg, 127, "Owner: %s", GetName(i));
            SendClientMessage(playerid, COLOR_PINK, aMsg);
            continue;
        }
    }
    return 1;
   
}
I also added continue to the statements, because they can only be one and there's no reason checking if they're three different admin levels when you've already found their admin level and sent what you've needed to, to the client.
Reply
#3

Just a quick version:

pawn Код:
CMD:admins(playerid, params[])
{
    new aMsg[35], Count = 0;
    SendClientMessage(playerid, COLOR_RED, "Administrators Online");
    foreach(Player, i)
    {
        if(pInfo[i][Adminlevel] < 1) continue;
        Count++;
        new Rank[15];
        switch(pInfo[i][Adminlevel])
        {
            case 1, 2: Rank = "Moderator";
            case 3: Rank = "Administrator";
            case 4: Rank = "Manager";
            case 5: Rank = "Owner";
            default: Rank = "Unknown";
        }
        format(aMsg, sizeof(aMsg), "%s: %s", Rank, GetName(i));
        SendClientMessage(playerid, COLOR_PINK, aMsg);
    }
    if(Count == 0) return SendClientMessage(playerid, COLOR_RED, "There are currently no Administrators online.");
    return 1;
}
Reply
#4

pawn Код:
new aMsg[35], Count = 0, i;
You forgot to define a new variable with name "i".
Reply
#5

try this
pawn Код:
CMD:admins(playerid, params[])
{
    new aMsg[MAX_PLAYER_NAME];
    SendClientMessage(playerid, COLOR_RED, "Administrators Online");
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
          if(pInfo[i][Adminlevel] == 1 && 2)
          {
              format(aMsg, 127, "Moderator: %s", GetName(i));
              SendClientMessage(playerid, COLOR_PINK, aMsg);
          }
          if(pInfo[i][Adminlevel] == 3)
          {
              format(aMsg, 127, "Adminisator: %s", GetName(i));
              SendClientMessage(playerid, COLOR_PINK, aMsg);
          }
          if(pInfo[i][Adminlevel] == 4)
          {
              format(aMsg, 127, "Manager: %s", GetName(i));
              SendClientMessage(playerid, COLOR_PINK, aMsg);
          }
          if(pInfo[i][Adminlevel] == 5)
          {
              format(aMsg, 127, "Owner: %s", GetName(i));
              SendClientMessage(playerid, COLOR_PINK, aMsg);
          }
       }
    }
    return 1;
}
Reply
#6

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
pawn Код:
new aMsg[35], Count = 0, i;
You forgot to define a new variable with name "i".
With foreach, you don't need to unless you use in the form of 'foreach(new i : Player)'. In this case, you can use 'foreach(Player, i)' and it will automatically define the variable. In the back of my mind, I remember someone calling this method outdated, but I have been too lazy to actually research it. I doubt it has any significant side effects because I have used that method in all of my codes. Others don't seem to encounter issues with it.

EDIT: By the way, the issue was the first 'if' statement was the only one being called in the loop. If he had of used 'else if' statements, this would of worked.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)