Using If/Then Statements
Using If/Then Statements
As you make games, you will find yourself wanting to make cause-effect relationships like the following:
- If a player scores 10 points, then they win the game.
- If a player has a power-up, then they can run super fast.
- If a player says “Happy Birthday” in chat, then sparkles appear.
All of these relationships need a conditional statement, lines of code that only run if certain conditions are true. One type of conditional statement is an if/then statement that checks to see if something is true before running instructions.
Practicing Conditional Statements
In this code example, a part will change transparency whenever the statement is true. Let’s practice with an obviously true statement like 3+3 = 6
. Since this statement is true, the next line of code will run.
Set up the Part and Script
-
In ServerScriptService, create a new script named IfThenScript.
-
Create a new part named IfThenPart.
-
Delete
Hello World
in the script and write a comment at the top.
Code the Condition
The condition in this case is if 3 + 3 is equal to 6, then something else will happen. To create this condition:
-
Type lowercase
if 3+3
-
After
3+3
, type==
Difference between == and =
- == compares values. It’s used for if statements.
- = sets values for variables
One way to remember which is which is that “compare” has two syllables, and two symbols. “Set” is one syllable and one symbol.
- After
==
, type6
. This is what you’ll check against. If you’re using other numbers, make sure the condition is true, like10 - 4 == 6
.
Code the Statement
After making the condition, the script needs a statement that’ll happen when that condition is true. In this case, the part will change transparency whenever the condition is true.
-
After if
3+3 == 6
, typethen
-
Press Enter to autocomplete the if statement with
end
-
Between
then
andend
, typegame.Workspace.BrickName.Transparency = .5
-
Test your code. Check that the part has changed transparency.
Troubleshoot Your Code
- Make sure
if
,then
, andend
are lowercase. - Use
==
, not a single=
. - Make sure that the code where you change the part’s transparency is between
if
andend
. If not, the statement may still work, but technically won’t be a part of the if/then statement.
Check a False Condition
Now, purposely change the statement to see what happens when the math equation is false.
-
Change
if 3+3 == 6 then
toif 3+3 == 10 then
-
Test your code now. The part will no longer change transparency because the statement is no longer true.
Making Other Comparisons
Instead of checking if two numbers are equal, you can also multiply, add, subtract, or make other comparisons. Depending on the games you make, one might be more useful than another.
Optional Challenge: Using Different Operators »
Instead of using ==
, complete these challenges using the Math and Comparison Operators table below:
- Checks if a number is less than 10
- Checks if a number is greater than or equal to 200
Checks if a number is less than 10:
if myNumber < 10
Checks if a number is is greater than or equal to 200:
if myNumber >= 200
Math Operators
Symbol | Meaning |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
Comparison Operators
Symbol | Meaning |
---|---|
== |
Is equal to |
~= |
Is not equal to |
<= |
Is less than or equal to |
>= |
Is greater than or equal to |
< |
Is less than |
> |
Is greater than |
These documents are licensed by Roblox Corporation under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Roblox, Powering Imagination, and Robux are trademarks of Roblox Corporation, registered in the United States and other countries.
Previous Using Functions Next Traps with if/then Statements