Conditional statements
Introduction
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
- The nil and boolean variable types
Setup
You will need to open Roblox Studio. Once you have done that, you will need to click "My ROBLOX", select your map, and click "Edit". At this point, you should see your familiar map.
You will need some essentials in Roblox Studio to make this work:
- The Output window:
- The Explorer window:
- 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.
Discussion
"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 Roblox 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.
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 if100 is not equal to 4, then tell us that (100~=4).
if 100~=4 then print ("100~=4") end
Scripting
Open Roblox 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 File: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)