Conditional statements

From Goodblox Wiki
Jump to navigationJump to search


This guide is for absolute beginners. It is intended to familiarize you with conditionals, or Conditional statements, in Lua. If you haven't already, please see Your first script as a beginner tutorial.

What this tutorial will teach you

  • What conditional statements are
  • Certain types of conditional statements
  • How to use conditional statements


You will need to open GoodBlox Studio. Once you have done that, you will need to click "My GoodBlox", select your map, and click "Edit". At this point, you should see your familiar map.

You will need some essentials in GoodBlox Studio to make this work:

  • The Output window:


  • The Explorer window:

Explorerwindow.JPG and

  • The Command bar:


  • To make sure you have the Output window, click View / Output.
  • To make sure you have the Explorer window visible, click View / Explorer.
  • To make sure you have the Command bar visible, click View / Toolbars / Command.


"Conditional statements are a way of responding differently based on the result of a comparison, called a conditional expression."[1]

This means that you are comparing the value of two (or more) things. Let's suppose we want GoodBlox if (2+3) is equal to 5, then tell us that (2+3) == 5 .

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

if (2+3) == 5 then print ("(2+3)==5") end

Always end an if/then statement with "end". "End" is like a period to a sentence.

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)>3") end

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

if 100~=4 then print ("100~=4") end


Open GoodBlox Studio and make sure that there aren't any other scripts in the Explorer window. We're going to start off with a fresh script.

  • Click Workspace in the Explorer Window.
  • Click Insert > Object > Script
  • Double click the newly inserted script.
  • A blank window should open up.
  • Insert the following script:
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

  • Test the script by pressing the play button Play.JPG
  • Your Output bar should show:

(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)


What happens if your statement is not true? That's when we can use an "else" statement. The "else" statement is similar to "otherwise" in English. For example, "If it is raining, bring an umbrella, otherwise (i.e., else) bring your suntan lotion."

Let's see this as applied to scripting:

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

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



If you want to execute a script if one of several conditions are true, we can use the elseif statement.

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

Since either "it is raining" or "it isn't raining" will be true, we can use the "elseif" statement. Let's try that in a script:

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

elseif (10>50)
	print("10 is greater than 50") 

elseif (10<100)
	print("10 is less than 100") 


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