attempt to perform arithmetic (add) on nil and number

This issue occurs when attempting to do calculations with a number and nil value. You most likely intended to do math between two numbers, but one of the values you thought was a number actually turned out to be nil.

If you’re unsure which value in the calculation is nil, then you can print all of them to find the nil value. Figuring out which value is nil is one of the biggest steps towards debugging this entire issue. Once you find the nil value, then you should look into why the value is nil.

We’ve seen this commonly occur for those following along with the Clicking Simulator series. So we’ll use this as an opportunity to show how we could debug it.

Lua
local Manager = {}

Manager.Profiles = {}

function Manager.AdjustClicks(player: Player, amount: number)
  local profile = Manager.Profiles[player]
  if not profile then return end
  
  print(profile.Data.Clicks) -- This will print either a number or nil
  print(amount) -- This will print either a number or nil
  
  profile.Data.Clicks += amount -- The error occurs on this line
  player.leaderstats.Clicks.Value = profile.Data.Clicks
end

return Manager

If profile.Data.Clicks is nil, then you likely forgot to add the Clicks as a key of your player data Template table.

If amount is nil, then you should investigate where you call this function from because you’re passing through a nil value for the amount argument.

Contents