Module 2 · Values and variables
Numbers and simple calculations
Work with whole and decimal numbers using the four basic arithmetic operators.
What you will learn
- Store whole and decimal numbers
- Use addition, subtraction, multiplication, and division
- Give calculated values meaningful names
- operator
- A symbol that performs an operation on values.
- expression
- Code that produces a value.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin totals, round time, shop prices, and movement measurements all depend on numbers and readable calculations.
The mental model
A numeric expression combines number values with operators. Luau evaluates it and gives the result back as another number.
Use + for addition, - for subtraction, * for multiplication, and / for division. Decimal values use a period.
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.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal coinsPerBag = 5local bagCount = 3local totalCoins = coinsPerBag * bagCountlocal halfShare = totalCoins / 2print(totalCoins, halfShare)Line by line
- 3
coinsPerBag stores a whole number.
- 4
bagCount is a second input to the calculation.
- 5
Multiplication produces the total number of coins.
- 6
Division may produce a decimal result.
- 8
Both results are sent to Output.
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: coinsPerBag stores a whole number.
Line 4: bagCount is a second input to the calculation.
Line 5: Multiplication produces the total number of coins.
Line 6: Division may produce a decimal result.
Line 8: Both results are sent to Output.
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 named results match the arithmetic you predicted without converting numbers to strings.
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 value does totalCoins receive?
Try it
Mark each step after you have tried it.
Common mistake
local totalCoins = "5" * 3The left value is text, not a number. Store quantities as numeric values without quotes before calculating.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Numbers can be whole or decimal values.
- Arithmetic operators produce new numeric values.
- Meaningful names make calculations readable.
Next: Next, you will represent yes-or-no state and the absence of a value.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.