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.
Hello Worldin 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:
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.
6. This is what you’ll check against. If you’re using other numbers, make sure the condition is true, like
10 - 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.
3+3 == 6, type
Press Enter to autocomplete the if statement with
game.Workspace.BrickName.Transparency = .5
Test your code. Check that the part has changed transparency.
Troubleshoot Your Code
- Make sure
==, not a single
- Make sure that the code where you change the part’s transparency is between
end. 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.
if 3+3 == 6 thento
if 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
||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