Module 4 · Making decisions
Using else
Choose exactly one of two possible paths.
What you will learn
- Add an else branch
- Explain why only one branch in the pair runs
- branch
- One possible path through conditional code.
- else
- The fallback branch used when the preceding condition is not truthy.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
A locked door should give useful feedback instead of doing nothing when the player lacks the key.
The mental model
if handles the truthy path. else handles every remaining case. Exactly one body in this pair runs during one decision.
The fallback has no condition because it represents whatever the first condition did not accept.
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.
else is read as “else”: it provides the fallback branch.
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 = falseif hasKey then print("The door opens")else print("You need a key")endLine by line
- 3
The current state is false.
- 5
The first branch asks about the key.
- 6
This body belongs to the true path.
- 7
else starts the fallback path.
- 8
The fallback gives actionable feedback.
- 9
One end closes the whole structure.
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: The current state is false.
Line 5: The first branch asks about the key.
Line 6: This body belongs to the true path.
Line 7: else starts the fallback path.
Line 8: The fallback gives actionable feedback.
Line 9: One end closes the whole structure.
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: One and only one message appears for each state.
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
Which message appears with hasKey false?
Try it
Mark each step after you have tried it.
Common mistake
Using a second separate if can allow two unrelated decisions to run. Use else when the paths are mutually exclusive parts of one question.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- else supplies a fallback.
- The two paths are mutually exclusive.
- Fallback messages can make game rules understandable.
Next: Next, you will order more than two possible paths with elseif.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.