Conditional statement

Conditional statements allow your script to perform specific actions when specific conditions are met.

Getting started

These conditions can be specified with relational operators, which are summarized below:

Relational Operators
Operator Meaning Example
< Less than 3 < 5 true
> Greater than 3 > 5 false
<= Less than or equal to 5 <= 5 true
>= Greater than or equal to 3 >= 5 false
== Equals 3 == 5 false
~= Does not equal 3 ~= 5 true

This means that you are comparing the value of two (or more) expressions. Let's suppose that if 2+3 is equal to 5, then we want Roblox tell us that "two plus three is five" .

Lua is similar to human language in this regard, we type:

if 2 + 3 == 5 then
    print ("two plus three is five")
two plus three is five

Always close an if/then statement with "end", as it marks the closing of the 'if' block.

An illustration of this in a flowchart would be:


Here's another example. Let's evaluate if (10-2) is greater than 3, then tell us that (10-2) > 3 .:

if 10-2 > 3 then
    print("10-2 is greater than 3")
10-2 is greater than 3

One last example. Evaluate if 100 is not equal to 4, then tell us that (100~=4).

if 100 ~= 4 then --the '~=' operator means 'not equal to'
    print("100 is not equal to 4")
100 is not equal to 4


Use the 'if' statement to execute a block of code if the inputted expression is true. In Lua, anything that is not nil or false is determined as true.

if 2 + 3 == 5 then print ("2+3==5") end
if 5 > 2 then print("5>2") end
if 2 < 5 then print("2<5") end
if 5 >= 5 then print("5>=5") end
if 5 <= 5 then print("5<=5") end
if 1 ~= 100 then print("1~=100") end
if "some text" then print("It worked") end
2+3==5 (because 5 is equal to 5)
5>2 (because 5 is greater than 2)
2<5 (because 2 is less than 5)
5>=5 (because 5 is greater than or equal to 5)
5<=5 (because 5 is less than or equal to 5)
1~=100 (because 1 is not equal to 100)
It worked (because "some text" is not nil or false)


In an 'if..else' block of code, the statements under the 'else' block will execute if the 'if' condition is false. Keep in mind that 'else' statements can only be used when there is an 'if' statement.

Let's see this as applied to scripting:

if 10 > 100 then 
    print("10 is greater than 100") 
    print("10 is less than 100") 
10 is less than 100

Since 10>100 is false, the then statement won't execute, but the else block will.



In an 'if..elseif' block of code, the compiler will go from top to bottom, checking whether each, if any, of the conditions are true. If one of the conditions are true, the corresponding block of code will be executed.

"If it is raining, bring your umbrella. Otherwise, if (i.e., "elseif") it is sunny, bring your suntan lotion."

We can use the "elseif" statement. Let's try that in a script:

if 10 > 100 then 
    print("10 is greater than 100") 
elseif 10 > 50 then	
    print("10 is greater than 50") 
elseif 10 < 100 then	
    print("10 is less than 100") 
10 is less than 100

The only instance any of the conditions would be true is 10<100, which would result in the printing of "10 is less than 100".


Note: Blocks of 'if..elseif..else' code can be used.

Advanced Usage

To eliminate the need for long and winding if statements, use the logical operators and and or to act as if/else and if/elseif...etc statements.

Here is an example that uses conditional expressions:

local variable = 1
print(variable == 1 and "The variable is equal to one."
                     or "The variable is not equal to one")
variable = 23
print(variable == 1 and "1"
   or variable == 2 and "2"
   or variable == 3 and "3"
   or                   "Something else")
The variable is equal to one.
Something else

For more information about and, or, and other logical operators, see the article on Boolean types.




After a refreshing day at the beach, Jerry wants to go eat dinner with his friends. There are three possible restaurants Jerry wants eat at, but he doesn’t know which ones are open. If pizzaPlaceIsOpen, Jerry wants to go to the Pizza Place. If it’s closed, however, Jerry will move on to see if tacoPlaceIsOpen. If it is, he wants to eat there. It it’s closed, he’ll see if hotdogStandIsOpen and go there if he can. Using what you learned in this tutorial, create a script with the variables above that determines where Jerry will eat and outputs the name of the restaurant or “Hungry!” if every restaurant is closed.

Here's some some code to test your output:

pizzaPlaceIsOpen = false
tacoPlaceIsOpen = true
hotdogStandIsOpen = false

--should output "Taco Place" with the above code
if pizzaPlaceIsOpen == true then
    print("Pizza Place")
elseif tacoPlaceIsOpen then  -- var == true is usually unnecessary when var is a boolean
    print("Taco Place")
elseif hotdogStandIsOpen then
    print("Hotdog Stand")