SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 04/03/2015, 01:02 PM   #1
$Marco$
Huge Clucker
 
$Marco$'s Avatar
 
Join Date: Sep 2012
Location: GetPlayerPos(playerid)
Posts: 360
Reputation: 127
Question Help with a loop. [EASY]

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?!
__________________
$Marco$ is offline   Reply With Quote
Old 04/03/2015, 01:08 PM   #2
Jimmy0wns
Gangsta
 
Join Date: Sep 2012
Location: Netherlands
Posts: 686
Reputation: 208
Default Re: Help with a loop. [EASY]

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.

Last edited by Jimmy0wns; 04/03/2015 at 01:08 PM. Reason: small spelling mistakes.
Jimmy0wns is offline   Reply With Quote
Old 04/03/2015, 01:12 PM   #3
$Marco$
Huge Clucker
 
$Marco$'s Avatar
 
Join Date: Sep 2012
Location: GetPlayerPos(playerid)
Posts: 360
Reputation: 127
Default Re : Re: Help with a loop. [EASY]

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.
__________________
$Marco$ is offline   Reply With Quote
Old 04/03/2015, 01:15 PM   #4
Schneider
Gangsta
 
Schneider's Avatar
 
Join Date: Jun 2007
Location: Netherlands
Posts: 515
Reputation: 136
Default Re: Help with a loop. [EASY]

@Jimmy:
- His code has proper indentation
- The 'random words' like "goto" and "do" are valid functions. http://wiki.sa-mp.com/wiki/Control_Structures#do-while

@Xysiaris:
You forgot to add the IsPlayerConnected(i) function.
__________________
My Releases:
- Keep your Lane (Ghost- & Traintrack-driving detector).
- Lottery/Keno (Gambling Minigame)
Schneider is offline   Reply With Quote
Old 04/03/2015, 01:17 PM   #5
Golimad
Huge Clucker
 
Golimad's Avatar
 
Join Date: Jan 2014
Location: Morocco
Posts: 251
Reputation: 36
Default Re : Help with a loop. [EASY]

Code:
new count;
for(new i=0; i<MAX_PLAYERS; i++)
{
    if(!IsPlayerConnected(i)) continue;
    count++;
}
Simple as this
Golimad is offline   Reply With Quote
Old 04/03/2015, 01:18 PM   #6
$Marco$
Huge Clucker
 
$Marco$'s Avatar
 
Join Date: Sep 2012
Location: GetPlayerPos(playerid)
Posts: 360
Reputation: 127
Default Re : Re: Help with a loop. [EASY]

Quote:
Originally Posted by Schneider View Post
@Jimmy:
- His code has proper indentation
- The 'random words' like "goto" and "do" are valid functions. http://wiki.sa-mp.com/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).
__________________
$Marco$ is offline   Reply With Quote
Old 04/03/2015, 01:20 PM   #7
Golimad
Huge Clucker
 
Golimad's Avatar
 
Join Date: Jan 2014
Location: Morocco
Posts: 251
Reputation: 36
Default Re : Help with a loop. [EASY]

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

Use For(new i ... )
You have break; and continue.
Golimad is offline   Reply With Quote
Old 04/03/2015, 01:22 PM   #8
$Marco$
Huge Clucker
 
$Marco$'s Avatar
 
Join Date: Sep 2012
Location: GetPlayerPos(playerid)
Posts: 360
Reputation: 127
Default Re : Help with a loop. [EASY]

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.
__________________
$Marco$ is offline   Reply With Quote
Old 04/03/2015, 01:26 PM   #9
Golimad
Huge Clucker
 
Golimad's Avatar
 
Join Date: Jan 2014
Location: Morocco
Posts: 251
Reputation: 36
Default Re : Help with a loop. [EASY]

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 
    
}
Golimad is offline   Reply With Quote
Old 04/03/2015, 01:28 PM   #10
$Marco$
Huge Clucker
 
$Marco$'s Avatar
 
Join Date: Sep 2012
Location: GetPlayerPos(playerid)
Posts: 360
Reputation: 127
Default Re: Re : Help with a loop. [EASY]

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.
__________________
$Marco$ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Easy; I want an easy tutorial for Y_INI MarcGonzales Scripting Help 2 05/02/2015 03:52 AM
[Ajuda] Loop parando a funcao (meio que nao continua depois do loop) Axll PortuguÍs/Portuguese 16 15/02/2014 05:34 PM
loop inside of loop? newbienoob Scripting Help 3 17/12/2013 04:06 AM
for( ) loop vs while( ) loop | While( ) loop is 143x faster? zgintasz Scripting Help 17 03/07/2012 12:57 PM
[Snippet]Easy loop(macro) yezizhu Help Archive 2 07/08/2009 02:15 PM


All times are GMT. The time now is 07:50 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.