Module 4 · Making decisions
Truthiness, nil, and guard clauses
Recognize false and nil as falsey, then preview early return as a flat failure path.
What you will learn
- Identify the two falsey Luau values
- Read a guard clause without deep nesting
- truthiness
- How a value behaves when used as a condition.
- guard clause
- An early check that exits when a requirement is not met.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
When Coin Quest cannot find a selected item, it should stop that action early instead of nesting the rest of the behavior inside several conditions.
The mental model
Only false and nil are falsey in Luau; values such as 0 and an empty string are truthy.
A guard clause checks a failure case first and returns. This is a preview of functions, which you will learn in Module 6.
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.
== is read as “equals equals”: it compares two values and produces a boolean.
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.
return is read as “return”: it ends the current function call and sends a value to its caller.
A tiny example
--!strictlocal function describeItem(selectedItem: string?): string if selectedItem == nil then return "No item selected" end return `Selected: {selectedItem}`endprint(describeItem(nil))Line by line
- 3
This small function receives text or nil and promises text back; functions are covered soon.
- 4
The guard explicitly checks the absent case.
- 5
return exits this call with useful feedback.
- 8
This line is reached only when an item exists.
- 11
The nil case is tested.
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
Start with the statements outside function bodies. A function definition creates a callable value; its body runs when a call, event, or scheduled task reaches it.
Trace the arguments at each call and the branch or loop inside that call. Return to the caller when the function returns. For an event, use the action named in Try it to trigger the callback.
Line 3: This small function receives text or nil and promises text back; functions are covered soon.
Line 4: The guard explicitly checks the absent case.
Line 5: return exits this call with useful feedback.
Line 8: This line is reached only when an item exists.
Line 11: The nil case is tested.
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: Both cases return one clear message through a flat path.
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 is returned for nil?
Try it
Mark each step after you have tried it.
Common mistake
if 0 then print("truthy")endProgrammers from some languages expect zero to be falsey. In Luau, zero is truthy; compare explicitly when zero has special meaning.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Only false and nil are falsey.
- Explicit nil comparisons communicate absence clearly.
- Guard clauses keep later code flatter.
Next: Next, you will assemble an ordered locked-door decision with useful feedback.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.