A conditional determines whether or not certain blocks of code will run. A conditional checks the value of an expression, called the condition. If the condition is
false, then the code block within the conditional will be skipped. Otherwise, the code block will be run.
Conditionals are used to control the flow of the execution of code within a program.
if conditional in Luau is given as follows:
if <condition> then<code block>end
Examples of basic
if true then --code block ranprint("code block ran")endif false then --no outputprint("code block ran")endif "chicken" then --code block ranprint("code block ran")end
To augment the basic
elseif can be used to check additional conditions if the first condition is
false. Also, the
else keyword can be used as a catchall to run a code block if all previous conditions were
if <condition 1> then <code block 1> elseif <condition 2> then <code block 2> else <code block 3> end
In the code above,
condition 1 is checked first.
condition 2 is checked only if
condition 1 is
false. If the first two conditions are both
code block 3 will be executed.
The following examples illustrate conditionals that use
if "tony" == "paulie" then --madone!print("ooh!")elseif "silvio" == "junior" thenprint("dooh!")elseprint("madone!")endif 3 >= 4 then --dooh!print("ooh!")elseif 23 thenprint("dooh!")elseprint("madone!")endif "AJ" == "AJ" and 3 < 10 then --ooh!print("ooh!")elseprint("dooh!")end