help with loop for(new loop
#1

Hello i made command to list all jailed players..

Like
for(new x = 0; x<MAX_PLAYERS; x++)
{
If(PlayerInfo[x][pjailed] == 1)
{
//list jailed players
}else{
//no players in jail
}
}
But when there are 5 players in server but not jailed it send me clientmessage 5 times if 2 players online then 2 times. Idk why i want to send only 1 time
Reply
#2

it's a loop, it loops through players and give the result every time, if you wanna check for no players in jail use this,
pawn Код:
for(new x = 0; x<MAX_PLAYERS; x++)
{
    if(PlayerInfo[x][pjailed] == 1)
    {
        //list jailed players
    }
    else if(x == MAX_PLAYERS -1)
    {
        //no players in jail
    }
}
Reply
#3

That's still wrong. It will list all jailed players and if the last slot isn't used it will also send the "no jailed" message. You need to keep a counter inside the loop which increments each time a jailed player is found. Then check if the counter is 0 when the loop is finished.
Reply
#4

Quote:
Originally Posted by Vince
Посмотреть сообщение
That's still wrong. It will list all jailed players and if the last slot isn't used it will also send the "no jailed" message. You need to keep a counter inside the loop which increments each time a jailed player is found. Then check if the counter is 0 when the loop is finished.
Like this ?
pawn Код:
new counter;
for(new x; x<MAX_PLAYERS; x++)
{
    if(PlayerInfo[x][pjailed] == 1)
    {
        counter ++;
        //list jailed players
    }
    if(x == MAX_PLAYERS -1 && !counter)
    {
        //no players in jail
    }
}
Reply
#5

No, like this:
pawn Код:
new count = 0;
for(new i = 0; i < MAX_PLAYERS; i++)
{
    if(!IsPlayerConnected(i)) continue;
    if(PlayerInfo[i][pjailed]) count++;
}
if(!count) SendClientMessage(playerid, -1, "No players in jail.");
Reply
#6

where to put show dialog codE?
Reply
#7

Thanks +rep
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)