Debug errors like a pro · Lesson 1/3 · 8 min
Read the error thoroughly
The FiveM error tells you the exact resource, file and line. Learn to read it at a glance.
Most people see a red error and panic. A pro reads it: it's almost always telling you exactly where to look.
SCRIPT ERROR: @mi_banco/server/main.lua:42: attempt to perform arithmetic on a nil value (local 'saldo')
> handler (@mi_banco/server/main.lua:42)A real error
- @mi_banco → the resource that's failing.
- server/main.lua:42 → file and exact LINE. Go straight there.
- attempt to perform arithmetic on a nil value → you tried to operate (+, -, *) with a nil.
- (local 'saldo') → the guilty variable is 'saldo': it was nil when you did the math.
Translation: on line 42, 'saldo' was nil when doing a math operation. Typical cause: the DB query returned nothing and you didn't check it. You go to line 42, see where 'saldo' comes from and add a guard.
Client console: F8. Server console: the txAdmin window or txAdmin → Live Console. These errors show up there in real time.
Practice what you learned
0/3Lees: "attempt to perform arithmetic on a nil value (local 'saldo')". ¿Qué significa?
Pista
'nil value (local saldo)' te da hasta el nombre de la variable culpable.
El saldo puede venir vacío de la BD. Completa el guard que evita el 'arithmetic on a nil value'.
local saldo = MySQL.scalar.await('SELECT money FROM users WHERE id = ?', { id })if not then return endlocal nuevo = saldo + cantidadPista
Comprueba la MISMA variable que el error señala como nil antes de usarla.
Peta con "attempt to index a nil value (local 'xPlayer')" porque el jugador no existía. Añade un guard antes de usar xPlayer.
Este código tiene un fallo:
local xPlayer = ESX.GetPlayerFromId(src)local cash = xPlayer.getMoney()Reescríbelo corregido:
Pista
ESX.GetPlayerFromId puede devolver nil; comprueba xPlayer antes de hacer xPlayer.algo.
Challenge: code it yourself
You're given: '@tienda/client.lua:18: attempt to call a nil value (field GetMoney)'. Say what it means and where to look.
Write it yourself in your editor (VS Code) and test it on your server. You learn here by doing it, not by copying.
See hint
A function/method called 'GetMoney' doesn't exist (typo? bad export?). Look at line 18 of client.lua.
Escribe aquí tu solución:
