attempt to perform arithmetic (add) on nil and numberin Roblox Luau: what it means and how to fix it
What it means
You added a number to a value that is nil. The word in brackets is the operation (add, sub, mul, div) and the two words after it are the types on the left and right.
The same error also shows up as:
- attempt to perform arithmetic (add) on number and nil
- attempt to perform arithmetic (sub) on nil and number
The smallest script that triggers it
local coins
coins = coins + 10
The fix
local coins = 0
coins = coins + 10
A variable with no value is nil. Starting it at 0 gives the addition a number to work with.
Common causes
- The variable was declared but never given a starting value.
- A data store returned nil for a new player and the result was used without a default such as `data or 0`.
- A table field or function argument that you expected to be a number was never set.
Practice this until it sticks
A Deep Dive on variables, nil and default values: 5-minute lessons, each with a quiz. What you miss comes back in the next module. One Deep Dive a day is free. No card.