Fix "Attempt to Perform Arithmetic on Nil" in Roblox Luau
What the Roblox error "attempt to perform arithmetic (add) on nil and number" means, how to find the nil value, and fixes for the causes behind it.
On this page
- How to read the error message
- Find the nil value with print
- Cause 1: An attribute that was never set
- Cause 2: tonumber() couldn’t convert the text
- Cause 3: Nothing saved in the data store yet
- Cause 4: A missing table field or a typo
- Cause 5: A function or remote that returns nothing
- Related message: “on Instance and number”
- If it still doesn’t work
- Related guides
- Sources
The Roblox error “attempt to perform arithmetic on nil” shows up when a script tries to add, subtract, multiply, or divide something that has no value. The message names the operation and tells you which side of it was empty, so you can fix it in a minute or two once you know how to read it. This guide explains the message and walks through the causes behind it.
How to read the error message
A typical error in the Output window looks like this:
ServerScriptService.CoinScript:8: attempt to perform arithmetic (add) on nil and number
It has four useful parts:
| Part | Example | What it tells you |
|---|---|---|
| Script path | ServerScriptService.CoinScript | Which script failed |
| Line number | 8 | The line with the bad math |
| Operation | (add) | Which operator failed: add is +, sub is -, mul is *, div is / |
| Types | nil and number | The left and right values, in the order they appear in your code |
So if line 8 is local total = coins + 10, then coins is nil. If the message said number and nil, the value on the right of the + would be the problem.
The operation only fails at the moment the line runs, so the real bug is usually somewhere earlier: the place where the variable should have been given a number but wasn’t.
Find the nil value with print
The quickest check is to print every value used on the failing line, right before it runs:
print("coins:", coins, typeof(coins))
print("bonus:", bonus, typeof(bonus))
local total = coins + bonus
typeof returns the type name as a string, so you’ll see nil, number, string, or Instance next to each value. Whichever prints nil is the one to trace back. Remove the print lines once you’ve found it.
flowchart TD
A["Read the line number and types"] --> B["Print the values on that line"]
B --> C{"Which type is wrong?"}
C -->|"nil"| D["Set a default or fix the source"]
C -->|"Instance"| E["Use the object's .Value"]
C -->|"string"| F["Convert with tonumber and check"]
Cause 1: An attribute that was never set
Attributes are a common source of this error. The Creator Hub states that Instance:GetAttribute() returns nil when no attribute with that name has been assigned. That happens if you spell the name differently in two places, read it before another script sets it, or never create it at all.
Fix it by treating “no attribute yet” as zero:
local coins = player:GetAttribute("Coins")
if coins == nil then
coins = 0
end
player:SetAttribute("Coins", coins + 10)
Here’s what each part does:
GetAttribute("Coins")reads the attribute and givesnilif it doesn’t exist.- The
ifblock swapsnilfor a starting value of0. SetAttributewrites the new total. The Creator Hub notes thatSetAttributecreates the attribute if it isn’t there yet, so the next read returns a number.
A shorter version is local coins = player:GetAttribute("Coins") or 0. It works because nil counts as false in a condition, so or falls back to 0. Also double-check that the attribute name matches exactly in every script, including capital letters.
Cause 2: tonumber() couldn’t convert the text
Text from a TextBox, a chat command, or a string stored somewhere is a string, not a number. People often run it through tonumber() first, which is the right idea, but the Luau library docs say tonumber returns nil when the conversion fails. Text like "abc" or an empty string fails, and the math on the next line throws the error.
local amount = tonumber(textBox.Text)
if amount == nil then
warn("Please enter a number")
return
end
print(amount * 2)
Check the result before you use it. If the input comes from a player, reject bad input with a message like this instead of quietly turning it into 0, so the player knows what went wrong.
Cause 3: Nothing saved in the data store yet
When you load player data, a brand-new player has nothing stored. The Creator Hub says GlobalDataStore:GetAsync() returns nil when the key doesn’t exist, so code like local coins = store:GetAsync(key) + 0 breaks the first time a new player joins.
local success, saved = pcall(function()
return store:GetAsync(key)
end)
local coins = 0
if success and saved ~= nil then
coins = saved
end
This keeps coins at 0 unless the call worked and there was saved data. If saving and loading misbehave only while you playtest, see why your DataStore isn’t working in Studio.
Cause 4: A missing table field or a typo
Reading a table key that doesn’t exist gives nil, and Luau won’t warn you about it until you do math with it. That covers typos (stats.Damge), different capitalization (stats.damage vs stats.Damage), and keys that only get added later in the game.
local stats = { Damage = 10, Speed = 16 }
local hit = (stats.Damage or 0) * 2
Fix the spelling if it’s a typo. If the field can really be missing, use or 0 as a default, as shown above.
The same goes for objects. Instance:FindFirstChild() returns nil when no child with that name exists, and if you then try to read a property from that nil you get a different message. That one is covered in how to fix “attempt to index nil with”.
Cause 5: A function or remote that returns nothing
A function that has no return, or only returns in some branches, gives nil to whatever called it:
local function getBonus(level)
if level > 5 then
return 20
end
return 0 -- without this line, low levels get nil
end
Make sure every path through the function returns a number. The same goes for arguments sent through remotes. If the client calls FireServer() without the value your server code expects, that parameter is nil on the server. Check what the client actually sends, and read why a RemoteEvent isn’t firing if the handler never runs at all.
Related message: “on Instance and number”
If the error says Instance instead of nil, you’re doing math on an object rather than a number. There are two common causes:
- Forgetting
.Value: with anIntValueorNumberValue(for example in leaderstats),coins + 10uses the object itself. Writecoins.Value + 10instead. - The player argument on the server:
RemoteEvent.OnServerEventalways passes thePlayerwho fired it as the first parameter. If your handler is written asfunction(amount), thenamountis actually the player. Write it asfunction(player, amount).
If it still doesn’t work
- Check timing. If one script sets a value and another reads it, the reader may run first. Set defaults where the value is created, such as in
PlayerAdded, or wait for the value before reading it. - Check for
Vector3or other types. Messages likeon Vector3 and numbermean you’re mixing a vector with a plain number. Do the math on a single component such asposition.Y, or build a newVector3from it. - Look at the whole stack trace. The first line names where the error happened, and the lines below it show which functions led there. That helps when the nil value came in as a function argument.
Default values solve most of these. If every number your game keeps (coins, levels, cooldowns) starts at a known value when it’s created, this error mostly goes away.
Related guides
- Fix “Attempt to Index Nil With” Errors in Roblox Studio
- Fix “Infinite Yield Possible” WaitForChild Warning in Roblox
- Roblox RemoteEvent Not Firing? How to Fix OnServerEvent
- Fix “X Is Not a Valid Member of Y” Errors in Roblox Studio
Sources
- Roblox Creator Hub: Instance (GetAttribute, SetAttribute, FindFirstChild)
- Roblox Creator Hub: Attributes
- Roblox Creator Hub: GlobalDataStore (GetAsync)
- Roblox Creator Hub: RemoteEvent (OnServerEvent)
- Roblox Creator Hub: nil
- Roblox Creator Hub: Operators
- Luau standard library (tonumber, typeof)
- DevForum: Attempt to perform arithmetic (sub) on nil and number
- DevForum: Attempt to perform arithmetic (add) on Instance and number
Frequently asked questions
What does "attempt to perform arithmetic on nil" mean in Roblox?
One side of a math operation such as +, -, * or / is nil instead of a number. The word in parentheses names the operation, and the two types that follow tell you which side was nil.
How do I know which value is nil?
The error lists the types in the same order as your code. "on nil and number" means the value on the left of the operator is nil, and "on number and nil" means the value on the right is. Print both values just before the failing line to confirm.
Why does GetAttribute give me nil?
The Creator Hub says GetAttribute returns nil when no attribute with that name has been assigned. Set a starting value with SetAttribute first, or fall back to a default such as 0 when the result is nil.
What does "attempt to perform arithmetic on Instance and number" mean?
You did math on an object, such as an IntValue, instead of the number stored inside it. Read its Value property instead, for example coins.Value + 10.