Module 4 · Making decisions
Your first if statement
Run a body only when its condition is truthy.
What you will learn
- Identify the condition and body of an if statement
- Use then and end to mark its structure
- condition
- An expression used to decide whether a branch runs.
- body
- The statements inside a control structure.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest should open a door only after the required fact becomes true. An if statement connects that fact to behavior.
The mental model
Luau evaluates the condition after if. When it is truthy, the body between then and end runs; otherwise that body is skipped.
Indentation helps people see the boundary, while then and end define it for Luau.
New words and symbols
--!strict asks Studio to check your code for type mistakes. It is an instruction for the editor and produces no Output message.
Matching quotes such as "hello" surround a string: a piece of text. The quotation marks tell Luau where the text starts and ends.
print(...) calls the built-in print function. Put the values to show inside its parentheses; separate multiple values with commas. Read the result in Studio's Output panel.
local is read as “local”: it creates a name that is available only in the current code scope.
if is read as “if”: it starts a decision by evaluating a condition.
then is read as “then”: it marks the body that can run when the condition is truthy.
end is read as “end”: it closes a block such as a decision, loop, or function.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal hasKey = trueif hasKey then print("The door opens")endLine by line
- 3
hasKey stores the current fact.
- 5
if begins the decision and then ends its condition.
- 6
This indented body runs only for a truthy condition.
- 7
end closes the if statement.
Read the code aloud
Read one statement at a time: name the value being read, what the operation does, and where its result goes. Keep identifiers exactly as written so you can find them again in the code.
Pause at calls, conditions, and loops. Explain when the body runs before moving to the next statement; use the line notes above when a symbol is unfamiliar.
Execution trace
Begin at the first executable statement. Comments and blank lines do not run; a branch chooses which block runs, and a loop may revisit its body.
Line 3: hasKey stores the current fact.
Line 5: if begins the decision and then ends its condition.
Line 6: This indented body runs only for a truthy condition.
Line 7: end closes the if statement.
State before and after
Before running, identify the starting values and any existing Studio objects this example needs. List what you expect to change.
After your practice change, check this result: The message appears only for the true condition.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
Predict before running
What happens while hasKey is true?
Try it
Mark each step after you have tried it.
Common mistake
if hasKey print("Open")endThe condition needs then before the body. Add it so Luau can parse the branch boundary.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- if begins a conditional branch.
- then separates condition from body.
- end closes the decision.
Next: Next, you will provide a second path for the false case.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.