Luau Scripting

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.

Fix "Attempt to Perform Arithmetic on Nil" in Roblox Luau
On this page
  1. How to read the error message
  2. Find the nil value with print
  3. Cause 1: An attribute that was never set
  4. Cause 2: tonumber() couldn’t convert the text
  5. Cause 3: Nothing saved in the data store yet
  6. Cause 4: A missing table field or a typo
  7. Cause 5: A function or remote that returns nothing
  8. Related message: “on Instance and number”
  9. If it still doesn’t work
  10. Related guides
  11. 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:

PartExampleWhat it tells you
Script pathServerScriptService.CoinScriptWhich script failed
Line number8The line with the bad math
Operation(add)Which operator failed: add is +, sub is -, mul is *, div is /
Typesnil and numberThe 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 gives nil if it doesn’t exist.
  • The if block swaps nil for a starting value of 0.
  • SetAttribute writes the new total. The Creator Hub notes that SetAttribute creates 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.

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 an IntValue or NumberValue (for example in leaderstats), coins + 10 uses the object itself. Write coins.Value + 10 instead.
  • The player argument on the server: RemoteEvent.OnServerEvent always passes the Player who fired it as the first parameter. If your handler is written as function(amount), then amount is actually the player. Write it as function(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 Vector3 or other types. Messages like on Vector3 and number mean you’re mixing a vector with a plain number. Do the math on a single component such as position.Y, or build a new Vector3 from 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.

Sources

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.

Related guides