attempt to concatenate a nil value
Happens when you join (..) a variable that is nil. We show you how to find which one and protect it.
The problem
The console shows «attempt to concatenate a nil value (local 'x')» and the script stops on that line.
The cause
You're joining with `..` a variable that is `nil` (data that didn't arrive: an empty DB field, an argument that wasn't passed, a getter that returned nil). Lua can't concatenate nil with text.
The solution
Check the value before concatenating and give it a default:
lua
-- Instead of:
local msg = 'Hello ' .. name -- name may be nil -> error
-- Protect with a default:
local msg = 'Hello ' .. (name or 'unknown')
-- Or validate first:
if not name then return end
local msg = 'Hello ' .. name
-- For numbers, convert them: tostring(value)
print('Balance: ' .. tostring(amount))Step by step
- 1.Look at the name in parentheses in the error (local 'x' / field 'y'): that's the nil variable.
- 2.Trace where it comes from: an empty MySQL query? an event argument that didn't arrive?
- 3.Protect with `(value or 'default')` or validate with `if not value then return end`.
- 4.To concatenate numbers use `tostring(n)`.
- 5.If it comes from the DB, make sure the row exists before using its fields.
Related guides
Last updated: 2026-06-25. Crxative-M is not affiliated with Cfx.re or Rockstar Games.
