Help with a loop. [EASY]
#1

So I made a loop to count throguh all the ONLINE players on the server, but it keeps counting to 500 no matter what (I was the only one on the server).

pawn Code:
new i;
i = 0;
loop:
if(i > MAX_PLAYERS) goto loop_stop;
if(something == 1)
{
   //do something
   i++;
   goto loop;
}
else
{
   i++;
   goto loop;
}
loop_stop:
//do something
return 1;
WHAT'S WRONG WITH THIS CODE?!
Reply
#2

Things that are wrong:
- Where the heck is the indentation? It's 2015 people.
- You already explained yourself what you did wrong in your code.
- The answer is in capital letters inside your code.
- Why are there random words there like "do something2;", "loop_stop" ?
- According to your signature, you appear to be a medium scripter, I don't know what medium means in your eyes. But whatever.
Reply
#3

Quote:
Originally Posted by Jimmy0wns
View Post
Things that are wrong:
- Where the heck is the indentation? It's 2015 people.
- You already explained yourself what you did wrong in your code.
- The answer is in capital letters inside your code.
- Why are there random words there like "do something2;", "loop_stop" ?
- According to your signature, you appear to be a medium scripter, I don't know what medium means in your eyes. But whatever.
Dear Jummy0owns, do something2 is an example for what gonna happen next, I have no intention of showing my code so I made an example code of what happens if the loop breaks and it reaches the MAX_PLAYERS on the server.

How about you show me what the error instead of giving me hints, its not a riddle forums its Scripting Help, so please - if you can't help me or have no intentions of helping me avoid posting, because I'm seeking for answers and not riddles.
Reply
#4

@Jimmy:
- His code has proper indentation
- The 'random words' like "goto" and "do" are valid functions. https://sampwiki.blast.hk/wiki/Control_Structures#do-while

@Xysiaris:
You forgot to add the IsPlayerConnected(i) function.
Reply
#5

Code:
new count;
for(new i=0; i<MAX_PLAYERS; i++)
{
    if(!IsPlayerConnected(i)) continue;
    count++;
}
Simple as this
Reply
#6

Quote:
Originally Posted by Schneider
View Post
@Jimmy:
- His code has proper indentation
- The 'random words' like "goto" and "do" are valid functions. https://sampwiki.blast.hk/wiki/Control_Structures#do-while

@Xysiaris:
You forgot to add the IsPlayerConnected(i) function.
I did add it on my real code it looks like this:

pawn Code:
new i;
i = 0;
loop:
if(i > MAX_PLAYERS) goto loop_stop;
if(something == 1 && isPlayerConnected(i) == 1)
{
   //do something
   i++;
   goto loop;
}
else
{
   i++;
   goto loop;
}
loop_stop:
//do something
return 1;
But it still counts to 500 and only then breaks instead of counting to 1 (or the number of players online on the server).
Reply
#7

Your mistake is : if(i > MAX_PLAYERS)
should be if(i < MAX_PLAYERS) I guess

Use For(new i ... )
You have break; and continue.
Reply
#8

Quote:
Originally Posted by Golimad
View Post
Your mistake is : if(i > MAX_PLAYERS)
should be if(i < MAX_PLAYERS) I guess
if(i > MAX_PLAYERS) goto loop_stop = if I is bigger than MAX PLAYERS go to label called loop stop.

if(i < MAX_PLAYERS) goto loop_stop = if I is smaller than MAX PLAYERS go to label called loop stop.

You are incorrect sadly.

I dont need to 'break' it as I send the code to somewhere else and it should STOP counting when it reaches MAX PLAYERS, but it don't.
Reply
#9

Quote:
Originally Posted by Golimad
View Post
Code:
new count;
for(new i=0; i<MAX_PLAYERS; i++)
{
    if(!IsPlayerConnected(i)) continue;
    count++;
}
Why don't you just use this to do the count and whatever function you wanna do to the players

You are making things complicated for nothing, What you are doing is pointless

Code:
new count;
for(new i=0; i<MAX_PLAYERS; i++)
{
    if(!IsPlayerConnected(i)) continue;
    count++;
    if(something) // code
    else(something) // code 
    
}
Reply
#10

Quote:
Originally Posted by Golimad
View Post
Why don't you just use this to do the count and whatever function you wanna do to the players
Because in the original code I have done things that require me to use it the way I showed it, I need someone just to tell me why i > MAX_PLAYERS is not working.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)