We use cookies on this site to enhance your user experience

Conditional Statements in Lua

Conditional Statements in Lua

Sep 25 2019, 1:59 PM PST 10 min

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

Getting started

These conditions can be specified with Articles/Operators#Relational|Relational Operators, which are summarized below:

Operator Meaning
< 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:

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

One last example. Evaluate if 100 is not equal to 4, then tell us that (100~=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.


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:

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:

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 Articles/Operators#Logical|Logical Operators and and or to act as if/else and if/elseif…etc statements.

Here is an example that uses Conditional Statements:

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


  • coding
  • lua