FANDOM


LoopsHere, I will explain three different types of loops. Loops are used to repeat something; sometimes forever, and sometimes just a certain amount of times.

WARNING: If you don't put a "wait()" into the loop, then when you run the script, you will crash the game.


'while' loops

'while' loops are used to repeat things an infinite amount of times, unless there is something in it that breaks, or returns the loop. That is more advanced though. The image to the right shows a very basic 'while' loop. In this one, there is a value (N) that is set to zero OUTSIDE the loop. INSIDE the loop it is saying that I want "N" to equal itself, plus 1. Then, wait one second and print the number representing "N" into the Output. 'while' loops require an "end" which tells the computer that the end is where the loop ends, but then goes back and repeats.

Hint: You don't have to indent your scripts like the picture; it's just easier to read.



'for' loops

These types of loops help you do something a certain amount of times. For example, the picture to the right shows a 'for' loop. First off, "N" equals zero. Then, it says "for i=1,100 do". This means for it to repeat 100 times. Then inside it waits 1 second, then adds 1 to "N" then prints "N'"'s value to the output. This type of loop is good for raising or lowering values a certain amount of time.

Hint: You don't have to indent your scripts like the picture; it's just easier to read.



'repeat' loops

This is the least used loop; a 'repeat' loop. These loops tell the computer to do something an infinite amount of times until a value reaches a certain point. For example, the picture to the right shows a loop just like all the other loops do; setting a value to zero, then making a loop adding 1 to the value and printing it into the output. The last line that says "until N == 10" means that it wants to repeat the loop until the value that represents N is equal to the value of 10.

Note: When there are two equal signs next to each other (==), it is looking for a value equal to another value.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.