Module 2 · Values and variables
Mini-project: Player profile values
Model a player's name, coins, alive state, and selected item with appropriately typed values.
What you will learn
- Choose a suitable value type for four profile fields
- Update values without changing their meaning
- Explain the profile through readable Output
- profile
- A related set of values describing a player.
- field
- One named piece of information in a larger model.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
These four values become the earliest data model for the Coin Quest player and will be reused when decisions, tables, UI, and persistence are introduced.
The mental model
Treat each local variable as one clear fact: a name is text, coins are numeric, alive state is boolean, and selected item may be text or absent.
The variables belong together conceptually even though you have not learned tables yet. Keeping them separate prevents a premature jump in complexity.
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 “plus equals”: it reads the current value, adds, then stores the result back.
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 playerName = "Ari"local coins = 0local isAlive = truelocal selectedItem: string? = nilcoins += 5selectedItem = "WoodenKey"print(playerName, coins, isAlive, selectedItem)Line by line
- 3
playerName stores text.
- 4
coins starts as a number that can be increased.
- 5
isAlive stores a boolean state.
- 6
selectedItem permits a string or no value.
- 8
The coin total updates from its previous value.
- 9
The absent item becomes a named item.
- 10
Output shows the resulting profile values.
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: playerName stores text.
Line 4: coins starts as a number that can be increased.
Line 5: isAlive stores a boolean state.
Line 6: selectedItem permits a string or no value.
Line 8: The coin total updates from its previous value.
Line 9: The absent item becomes a named item.
Line 10: Output shows the resulting profile values.
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 server Script reports a coherent profile, and the optional Studio check recognizes the four local declarations.
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 coin value is printed after the update?
Try it
Mark each step after you have tried it.
Common mistake
Do not use the same type merely because values are printed together. A coin count stored as text cannot participate naturally in later calculations.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Choose value types according to meaning.
- A profile can begin as several focused local variables.
- Output helps verify updates before UI exists.
Next: Module 3 builds expressions that compare, combine, and format these values.
Studio challenge
Player profile values
Checks are formative feedback, not cheat-proof certification. You can finish this lesson without the plugin.
- Target
- ServerScriptService/LearningLuau/PlayerProfile
- Check mode
- source and output
Challenge state: idle
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.