SA-MP Forums Archive
foreach doesnt work - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: foreach doesnt work (/showthread.php?tid=604671)



foreach doesnt work - NeXoR - 08.04.2016

Used on the /backup system
PHP код:
CMD:bk(playeridparams[])
{
    new 
string[128];
       if(!
IsPlayerLoggedIn(playerid) || PlayerInfo[playerid][pAsshole] == 1) return SendClientMessage(playeridCOLOR_GREY"You are not allowed to use this command.");
    if(!
IsALeo(playerid) && !IsMedic(playerid)) return SendClientMessage(playeridCOLOR_GREY"You are not a government official.");
    if(
sscanf(params"s[128]"params)) return SendClientMessage(playeridCOLOR_WHITE"USAGE: /backup [LEO/MEDIC]");
    if(!
strcmp(params"leo"true))
    {
        if(
Backup[playerid] == 1) return SendClientMessage(playeridCOLOR_GREY"You have already requested for police backup.");
        
Backup[playerid] = 1;
        foreach(new 
Player)
        {
            if(
IsACop(i) || IsSASD(i) || IsFBI(i))
            {
                
SetPlayerMarkerForPlayer(iplayerid0x0000FFFF);
                
format(stringsizeof(string), "[Central Dispatch]: %s %s is requesting immediate backup, his coordinates have been marked on your GPS."RPFRN(playerid), RPN(playerid));
                
SendClientMessage(iCOLOR_RADIOstring);
                
printf("The number is %d.",i);
                return 
1;
            }
        }
    }
    if(!
strcmp(params"medic"true))
    {
        if(
Backup[playerid] == 2) return SendClientMessage(playeridCOLOR_GREY"You have already requested for medical assistance.");
        
Backup[playerid] = 2;
        foreach(new 
Player)
        {
            if(
IsMedic(i))
            {
                
SetPlayerMarkerForPlayer(iplayerid0xFF3366FF);
                
format(stringsizeof(string), "[Central Dispatch]: %s %s is requesting medical assistance, his coordinates have been marked on your GPS."RPFRN(playerid), RPN(playerid));
                
SendClientMessage(iCOLOR_RADIOstring);
                
printf("The number is %d.",i);
                return 
1;
            }
        }
    }
    return 
1;

I debugged it with printf - ID is 0 (aka nobody)


Re: foreach doesnt work - Akbaig - 08.04.2016

Try using this instead:


PHP код:
foreach(Playeri)
{
//your code




Re: foreach doesnt work - NeXoR - 08.04.2016

Quote:
Originally Posted by Akbaig
Посмотреть сообщение
Try using this instead:


PHP код:
foreach(Playeri)
{
//your code

Won't compile
I have already updated my includes


Re: foreach doesnt work - Akbaig - 08.04.2016

Damn I get it now..

You've used the

PHP код:
return 1
in the loop.. which disables the loop to repeat.


Re: foreach doesnt work - NeXoR - 08.04.2016

Quote:
Originally Posted by Akbaig
Посмотреть сообщение
Damn I get it now..

You've used the

PHP код:
return 1
in the loop.. which disables the loop to repeat.
stupid me, thanks


Re: foreach doesnt work - Crayder - 08.04.2016

Quote:
Originally Posted by Akbaig
Посмотреть сообщение
Try using this instead:


PHP код:
foreach(Playeri)
{
//your code

For future reference...

Don't use this! That's an outdated format! Your current code is fine, but as mentioned above (and as you fixed) you are returning prematurely.