If Statements & Loops

A tutorial on conditional statements and loops

by kitrank

Author Avatar

Update 9/18/19: Henlo, just wanted to say a great big thank you to all the viewers and commenters for supporting this tutorial! I have recieved a lot of compliments on this tutorial for being easily understandable (and entertaining) which motivates me to make more. So once again, thank you all for the support even though my writing is terrible!

What are If Statements?

If Statements are conditional statements that will proceed to run code only if the CONDITION is true.

if CONDITION then
    -- Run code here
end

The CONDITION is just a fancy term for whatever the if statement is checking to be either true or false.

What should the condition be? Well let's say hypothetically you had a store in your game and a player had to buy something. The if statement in this scenario would be to check if the player had enough money to buy the item from the store and the condition would determine if the player's money was equal to or greater than the amount of money required to buy the item.

if Player_Money >= Item_Price then
    -- The Player recieves the item and their money is taken
end

Notice the >=? This is called an operator, or you can just call it a math sign. I mention this because the operators you use in Lua are a bit different than the ones you use for math. For example, you might use = to check if two numbers are equal however in Lua it is actually ==. Below is a list of operators and their respective operation.

Operators == | Equal ~= | Not Equal

False Conditions

If a condition returns false then the code will not run, unless you are checking if the condition is false. To check if a condition is false, you use either not or ~= depending on the value that is being checked. not is for true/false values and ~= is for comparing strings or numbers.

Below is an example of an if statement that will not run.

if 2 + 2 == 5 then
    print("2 + 2 = fish")
    -- Doesn't run because 2 + 2 does not equal 5 (or fish)...
end

Below are two examples checking if the condition is false instead of true.

-- Example 1
if 2 + 2 ~= 5 then
    print("Does it work?")
    -- Yep because the condition that 
    -- 2 + 2 does NOT EQUAL 5 is true!
end

-- Example 2
local AmICool = false -- Example true/false value
if not AmICool then
    print("forever a loser :(")
end

If/Else Statement

An If/Else Statement is used to run alternative code if the condition is false.

if 2 + 2 = 5 then
    print("Does it work this time tho?")
    -- Nope, 2 + 2 is still 4!
else
    print("Epic gamer moment!")
    -- Runs instead of the first code because
    -- the condition was false.
end

If/Elseif Statements

If/Elseif Statements will only run alternative code if the second if statement's condition is true.

if 2 + 2 == 5 then
    print("Oof! I am still not true yet... Will I ever be?")
elseif 2 + 2 = 4 then
    print("Meow")
end

While Loop

While Loops keep repeating or looping code while the condition is true.

   while 1 + 2 = 3 do
       print("Output spam, woohoo!")
       wait()
   end

Whenever using a while loop there are two things to remember!

  1. Always include a wait() inside! Without one the game will freeze.
  2. Keep it at the bottom of the script because it will yield the script or prevent it from running the code below until it finishes running.

Repeat Loop

Repeat Loops keep repeating or looping code until the condition is true.

    repeat print("Output spam forever!") until 2 + 2 == 5

For Loop

For Loops repeat or loop code a set number of times.

    for i = 1, 10 do
    
    print("Will print 10 times!")
    end

1 is the starting value that will increase by 1 (by default) each loop that runs. Once the starting value reaches 10 it will stop running.

    for i = 5, 20 do
        print("Goodbye world!")
    end

The above for loop would run a total of 15 times since it starts at 5 and goes up to 20.

Additionally, you can add the increment amount.

    for i = 1, 100, 20 do
        print("Yeet")
    end

The 20 is the increment amount. This for loop would run a total of 5 times since it starts at 1, goes up by 20, and goes to 100.

View in-game to comment, award, and more!