How to Script Anything/Chapter2

From Legacy Roblox Wiki
Jump to navigationJump to search

Chapter 2: Flow Control

Especially in Roblox, if there was no way of controlling what happens, there would be very little that could be done. One way of controlling the "flow", or what the script executes next, is by using events (in Chapter 3).

While Loops

A while loop uses the syntax

while ''condition is true'' do
[Statements]
[break]
end

Using the "break" command is optional. It will exit any loop that it is in. This applies to all loop commands.
The "condition" is an expression that can be evaluated to "true" or "false"; boolean values.
What does the loop do? If the condition is true, it evaluates all the statements in the loop's block until a "break" command is executed or the condition is true when the loop restarts.

For Loops

There are a few more advanced for loops, but the most basic one looks like this:

for ''variable'' = ''startValue'', ''endValue''[,{''increment''}] do
''Statements''
[break]
end

The "step" parameter is optional. Examples:

for i = 1, 10 do
print(i) --prints 1 through 10
end
for i = 3, 1, -0.5 do
print(i) --prints 3 through 1 in increments of -0.5: 3, 2.5, 2, 1.5, 1
end

In other words, a for loop will increase the variable you specify starting at the startValue and continuing to add the increment to the variable until the variable is equal to (or past) the endValue.

NOTE: Clarification should be used on exactly when the for loop exits in something like "for i = 1, 5, 2". Does it stop when i = 4, or i = 6 ?

If/Then Statement

If / then statements are very important. Without them, computers couldn't do anything. The syntax is:

if ''condition is true'' then 
''Statements'' 
[elseif ''condition'' then 
''Statements'' 
[elseif...]] 
[else ''Statements''] 
end

What this means is that you start with a boolean condition. If it is true, the statements between the word "then" and the next "else", "elseif", or "end" are executed. Then all other statements in that block are skipped. If the condition is false and there is an elseif statement, its condition will be checked. This process continues until an 'else' or 'end' command is encountered. "else" is run if none of the conditions checked are true. Note that elseif and else are optional.
An example helps

myNum=6 --your guess, try editing it
realNum=10 --the number you are supposed to "guess". Try editing it

if realNum==myNum then
print("You guessed it!")
elseif myNum < 0 or myNum > 100 then
print("The number should be between 0 and 100.")
elseif realNum > myNum then
print("Your guess is too small.")
elseif realNum < myNum then
print("Your guess is too large.")
else
print("BUG! This is impossible!")
end

Note that the line that prints out "BUG!" will never run, and should be deleted. This could be used for debugging purposes to make sure that you have covered all the logical possibilities. Here a flow chart could be used to visually show that all possibilities are covered. However, reasoning works just as well. Note that if you assign -5 to "myNum", making the line read "myNum=-5", you might wonder what the result is. The logic is as follows:
"realNum==myNum becomes 10==-5". This is not true, so Lua advances to:
"myNum < 0 or myNum > 100", which becomes "-5 < 0 or -5 > 100", which results in "true or false", which is true. This means that the line "print("The number should be between 0 and 100.")" will run. Note that Lua will skip all the remaining checks and go straight to the end, even though "realNum>myNum" (10 > -5) is true. Remember that order counts when making your if/then/elseif structure.

Try it out!

Open up a blank script and try making a few simple scripts:

  • A script that prints out all the numbers between 1 and 20 and at the same time tells you if the number is even or odd:


Output would look like this:
1 odd
2 even
3 odd
4 even

  • A script that takes 2 variables, as seen in the above if/then example, and tests to see if 2 times one of the variables is larger than the other.
  • A script that prints out all the numbers from 3 to 13, skipping 5 and 8.
  • A script that prints out all the numbers from 5 to 1 in increments of -0.2.
  • A script that tests to see how many times 2 variables are equal to eachother. You will need the command:


math.random(min, max) --returns a "random" integer between min and max. It is not a truly random number, but it works well enough for this
Once you get your script to test to see if 2 different random numbers are equal, ensure that you record whether they are equal or not and run the test 10 times before printing out the results. Output might be:
"Number of times they matched: 3
Number of times they didn't match: 17"
Try different min and max values for the random number generator. Also try making them different for each variable.


Back to How to Script Anything