Welcome back. You already know how to create a numeric variable, print its value, and use type() to confirm it is a number. Now you will make those numbers useful: scripts can calculate with them and replace an old value with a new one.
By the end of this lesson, you will be able to calculate rewards, costs, and totals using arithmetic operators, then update a variable such as a player’s coin balance. This is the basis for scores, health, timers, shop prices, and many other Roblox systems.
Arithmetic expressions: calculations in code
An arithmetic operator is a symbol that tells Luau to do math with numbers. The most common ones are:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ | Add | 7 + 3 | 10 |
- | Subtract | 7 - 3 | 4 |
* | Multiply | 7 * 3 | 21 |
/ | Divide | 7 / 2 | 3.5 |
% | Find the remainder after division | 13 % 5 | 3 |
A calculation such as 7 + 3 is called an expression. Luau works out the expression and uses its result.
Put arithmetic directly inside print() to see it in Output:
print(7 + 3)
print(12 - 5)
print(4 * 6)
print(15 / 3)
Output:
10
7
24
5
You can also store a calculation in a variable:
local startingCoins = 20
local questReward = 15
local totalCoins = startingCoins + questReward
print(totalCoins)
Output:
35
Luau first calculates startingCoins + questReward, then stores the answer, 35, in totalCoins.
Roblox Luau Course - Episode 1: Fundamentals
Watch “Roblox Luau Course - Episode 1: Fundamentals” by Hoofer for a visual demonstration of using number variables in calculations and then changing their values.
Watch calculations and updates. Pay particular attention to the difference between creating a new calculated value and changing the value already stored in a variable. The later operators shown in the segment are not essential to memorize today.
Numbers only: do not calculate with text
Arithmetic operators work with numbers, not strings.
This works because both values are numbers:
local coins = 10
local reward = 5
print(coins + reward)
This does not represent a numeric reward:
local reward = "5"
The quotation marks make "5" a string. It looks like a number to a person, but Luau treats it as text. If you are unsure, use the tool from the previous lesson:
local reward = "5"
print(type(reward))
Output:
string
For scores, coin totals, health, and prices, store values without quotation marks:
local reward = 5
Division and remainders
Division with / can produce a decimal:
local coinsPerPlayer = 15 / 2
print(coinsPerPlayer)
Output:
7.5
The % operator is called modulus. It gives the amount left over after a division.
local leftoverCoins = 15 % 2
print(leftoverCoins)
Output:
1
If 15 coins are split evenly between 2 players, each player could receive 7 coins and 1 coin would remain. Modulus is useful whenever you need to track a remainder, such as leftover items or a repeating pattern.
For now, focus on recognizing its meaning:
print(10 % 3) -- 1 remains after splitting 10 into groups of 3
print(12 % 3) -- 0 remains because 12 divides evenly by 3
More than one calculation: use parentheses when needed
Luau follows the usual math order: multiplication and division happen before addition and subtraction.
local result = 10 + 3 * 2
print(result)
Output:
16
Luau multiplies 3 * 2 first, giving 6, then adds 10.
Use parentheses when you want a different order:
local result = (10 + 3) * 2
print(result)
Output:
26
Here Luau calculates the parentheses first: 10 + 3 is 13, then 13 * 2 is 26.
Parentheses are especially helpful when your calculation represents a game rule. They make the intended logic easier for you to read later.
Updating a variable
Creating a variable gives it an initial value:
local coins = 20
But a game needs values to change. A player earns coins, spends coins, takes damage, or receives a score bonus.
To update a variable, write its name on the left side of = and calculate its new value on the right:
local coins = 20
coins = coins + 5
print(coins)
Output:
25
Read this as:
“Take the current value of
coins, add 5, and store the result back incoins.”
The same variable name appears twice, but it has two different jobs:
- On the right,
coinsmeans “use the current value,” which is 20. - On the left,
coinsmeans “replace the stored value with the new answer,” which is 25.
The = sign is an assignment instruction here. It does not mean that both sides are permanently mathematically equal.
Do not write local again when updating:
local coins = 20
coins = coins + 5
Writing this would try to create a new local variable with the same name:
local coins = 20
local coins = coins + 5
Use local once when you first create the variable. After that, update it by using its name.
Compound assignment: the short version
Luau provides shorter update operators called compound assignment operators.
These two versions do exactly the same thing:
coins = coins + 5
coins += 5
The compact version is common in Roblox scripts because it makes changing a value easier to scan.
| Full update | Short update | Meaning |
|---|---|---|
coins = coins + 5 | coins += 5 | Add 5 coins |
coins = coins - 5 | coins -= 5 | Remove 5 coins |
coins = coins * 2 | coins *= 2 | Double the coins |
coins = coins / 2 | coins /= 2 | Split coins in half |
For game values, the most frequent ones are += for rewards and -= for costs or damage.
Operators | Documentation - Roblox Creator Hub
Read the official Roblox Creator Hub explanation of compound assignment. It confirms the shorthand patterns you will use frequently in Roblox scripts.
In the Compound assignment section, begin at the explanation of compound assignment. Then study the table immediately below it, focusing on the +=, -=, *=, and /= rows. You may notice additional operators in the table; recognize that they exist, but concentrate on the four basic numeric updates for this lesson.
Guided mini-project: a quest reward total
Create or reuse your PracticeScript. This short script simulates a player completing a quest, buying an item, and receiving a bonus.
-- Calculate and update a player's coin total.
local coins = 10
local questReward = 25
local potionCost = 8
local bonusCoins = 3 * 4
coins += questReward
coins -= potionCost
coins += bonusCoins
local coinsPerPlayer = coins / 2
local leftoverCoins = coins % 2
print("Final coin total:")
print(coins)
print("Coins if split between two players:")
print(coinsPerPlayer)
print("Coins left over after the split:")
print(leftoverCoins)
Run the script. Your Output should show:
Final coin total:
39
Coins if split between two players:
19.5
Coins left over after the split:
1
Follow the changing value of coins carefully:
| Moment in the script | Calculation | coins |
|---|---|---|
| Initial amount | — | 10 |
| Quest reward | 10 + 25 | 35 |
| Potion cost | 35 - 8 | 27 |
| Bonus | 27 + 12 | 39 |
The bonusCoins variable also contains a calculation:
local bonusCoins = 3 * 4
Luau stores 12 in bonusCoins, not the text 3 * 4.
Make one small change after the script works: change potionCost from 8 to 15, run it again, and observe how the later calculations automatically use the new total. This is why variables are useful: you can adjust one value without rewriting every calculation.
A quick checking habit
When a calculation produces an unexpected result, print the important values before and after an update:
local health = 100
local damage = 30
print(health)
health -= damage
print(health)
Output:
100
70
This lets you verify three things:
- The variable began with the value you expected.
- The calculation used the intended number.
- The updated value was stored successfully.
If you see an error involving arithmetic, check whether you accidentally put quotation marks around a number. Then use type() if necessary to verify that each value is a number.
Key takeaways
Arithmetic operators let Luau calculate with numbers:
+adds.-subtracts.*multiplies./divides.%finds a remainder.
You can store a calculated result in a new variable:
local totalCoins = startingCoins + reward
You can replace a variable’s old value with a new calculated value:
coins = coins + 5
Or use the shorter compound-assignment form:
coins += 5
coins -= 5
Keep game quantities as numbers, without quotation marks. Soon, you will use these calculations to build clearer Output messages by combining text with variable values.
Can't find a good explanation? Sign up and we'll make it for you
Sign up