First steps, lesson 4
Numbers and math
Goal: Calculate scores and rewards.
about 5 min4 practice exercises
Luau does arithmetic the way a calculator does.
print(7 + 2) -- 9
print(7 - 2) -- 5
print(7 * 2) -- 14
print(7 / 2) -- 3.5
print(7 // 2) -- 3 (divide, then round down)
print(7 % 2) -- 1 (the remainder)
print(2 ^ 3) -- 8 (2 to the power of 3)
Multiplication and division happen before addition and subtraction, so 5 + 3 * 2 is 11. Use brackets to change the order: (5 + 3) * 2 is 16.
The remainder operator % is more useful than it looks. coins % 10 == 0 is true every tenth coin, which is how games give a bonus "every 10 coins".
local coins = 0
local coinValue = 5
coins += coinValue -- one pickup
coins += coinValue * 2 -- a double coin
print(coins) -- 15
Sources
Checked against Roblox's documentation on . The lesson is in our own words; these pages are the source of truth.