attempt to perform arithmetic on a nil value
This Lua error pops up when you do a math operation with a variable that is nil. We explain how to find the culprit and harden it.
The problem
The console shows "attempt to perform arithmetic on a nil value" and the script breaks right on an addition, subtraction or multiplication.
The cause
One of the operands is nil: a Config value that doesn't exist, a database query that returned no row, or a typo in the variable name.
The solution
Validate the value before operating and give it a default:
lua
-- BAD: if Config.Precio is nil, it crashes
local total = Config.Precio * cantidad
-- GOOD: default value + clear warning
local precio = Config.Precio or 0
if precio == 0 then
print('^3[mi_recurso] Config.Precio is not defined^0')
end
local total = precio * cantidadStep by step
- 1.Look at the file and line the console points to: that's where the operation is.
- 2.Check that every variable in the operation has a value (Config, MySQL result, event parameter).
- 3.Use `variable or 0` for numbers that might be missing.
- 4.If it comes from the database, make sure the query returned a row before using the data.
Related guides
Last updated: 2026-06-17. Crxative-M is not affiliated with Cfx.re or Rockstar Games.
