Module 3 · Expressions, operators, and text
Updating values with compound assignment
Use +=, -=, *=, and /= when a value changes from its current state.
What you will learn
- Apply four compound assignment operators
- Explain the previous-value relationship
- compound assignment
- An operator that calculates from and reassigns a variable in one statement.
Loading saved progress…
Understand
Read the idea and follow a worked example.
Why this matters
Coin counts rise, timers fall, multipliers grow, and shared rewards divide. Compound assignment states those updates directly.
The mental model
Read coins += 5 as: “Take the current value in coins, add 5, and store the result back in coins.” Luau reads 10, calculates 10 + 5 as 15, then stores 15.
The longer equivalent is coins = coins + 5. The coins on the right reads the old value, + 5 calculates a new value, and = stores that new value into the name on the left. This line runs once each time execution reaches it.
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 “plus equals”: it reads the current value, adds, then stores the result back.
= is read as “is assigned”: it evaluates the right side and stores that value in the name on the left.
A tiny example
--!strictlocal coins = 10coins += 5print(coins)Line by line
- 3
Before the update, coins stores the number 10.
- 5
Step 1 reads 10, step 2 calculates 10 + 5 as 15, and step 3 stores 15 back in coins.
- 7
Output reads the new stored number: 15.
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: Before the update, coins stores the number 10.
Line 5: Step 1 reads 10, step 2 calculates 10 + 5 as 15, and step 3 stores 15 back in coins.
Line 7: Output reads the new stored number: 15.
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: You can name the value after each update and verify it in Output.
Reading or printing a value leaves it unchanged. Look for assignments, calls that change state, and property updates to explain each change you observe.
The same update written in full
--!strictlocal coins = 10coins = coins + 5local health = 100health -= 20Predict before running
What is stored in coins after it starts at 12 and then runs coins += 3?
Try it
Mark each step after you have tried it.
Common mistake
local coins = "10"coins += 5Quotation marks make "10" a string. It looks numeric to a person, but Luau treats it as text, and numeric addition needs a number. Remove the quotation marks when the value represents an amount.
Checkpoint
Submit once you have considered every question. Retries are unlimited.
Recap
- Compound assignment reads the current value.
- It calculates and stores in one statement.
- Choose the operator that matches the game update.
Next: Next, you will compare two values and receive a boolean result.
Studio Companion
Not connected
Connect once. Compatible challenges will prepare automatically as you reach them.