Module 2 · Values and variables
Booleans and nil
Represent true-or-false state and distinguish an absent value from text that looks similar.
What you will learn
- Store a boolean state
- Use nil to represent absence
- Distinguish boolean and nil values from quoted text
- boolean
- A value that is either true or false.
- state
- Information describing a system at a particular moment.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin Quest needs to know whether a door is unlocked and whether the player has selected an item. Those are states, not messages.
The mental model
A boolean is a two-state value: true or false. It is useful for facts that can be answered yes or no.
nil means a value is absent. The strings "true" and "nil" are ordinary text and do not carry those meanings.
Read selectedItem: string? as selectedItem may hold text or no value. The colon introduces a type note and the question mark allows nil. The = nil after it supplies the current value. This small preview lets the strict editor accept an item that starts empty and later receives text.
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.
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.
A colon in a declaration, such as coins: number, introduces a type annotation: the kind of value that name is allowed to hold.
? is read as “optional”: it marks a type that may also be nil.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal isDoorUnlocked = falselocal selectedItem: string? = nilprint(isDoorUnlocked, selectedItem)Line by line
- 3
false is a boolean representing the locked state.
- 4
The question mark allows the string variable to have no value; nil supplies that absence.
- 6
Both current states are printed for inspection.
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: false is a boolean representing the locked state.
Line 4: The question mark allows the string variable to have no value; nil supplies that absence.
Line 6: Both current states are printed for inspection.
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 state variables change type-safely from locked to unlocked and absent item to named item.
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 assignment means no item is currently selected?
Try it
Mark each step after you have tried it.
Common mistake
local isDoorUnlocked = "false"This stores non-empty text, not a false boolean. Remove the quotes when representing a true-or-false state.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Booleans represent true-or-false state.
- nil represents absence.
- Quoted lookalikes are strings, not booleans or nil.
Next: Next, you will assemble four player profile values in a complete mini-project.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.