Debugging and securing your resources · Lesson 1/3 · 10 min
Debugging like a pro: errors, prints and resmon
Read the error, place strategic prints and hunt the lag with resmon: the complete debugging kit.
Most people see a red error and panic. A pro reads it: it almost always tells you exactly where to look.
SCRIPT ERROR: @mi_banco/server.lua:42: attempt to perform arithmetic on a nil value (local 'saldo')A real error
- @mi_banco → the resource that fails.
- server.lua:42 → exact file and LINE. Go straight there.
- attempt to perform arithmetic on a nil value → you operated (+, -, *) with a nil.
- (local 'saldo') → the guilty variable is 'saldo': it was nil when doing the math.
Strategic prints (with a switch)
Config.Debug = true
local function dbg(...)
if Config.Debug then print('[mi_banco]', ...) end
end
RegisterNetEvent('banco:retirar', function(cantidad)
local src = source
dbg('withdraw requested by', src, 'amount:', cantidad)
local Player = QBCore.Functions.GetPlayer(src)
dbg('Player exists?', Player ~= nil)
end)Conditional debug
Hunt the lag with resmon
# In the client console (F8):
resmon
# Lists each resource and its CPU (ms) per frame.
# Green < 1ms = good. Yellow/red = that resource is eating your FPS.resmon
Place prints BEFORE and AFTER the suspicious point: if the first one shows and the second doesn't, the failure is in between. And when you're truly stuck, paste the error and the code into the Crxative-M chat: it gives you the root cause and the fix without wasting your afternoon.
Practice what you learned
0/3Lees en consola: 'SCRIPT ERROR: @mi_banco/server.lua:42: attempt to perform arithmetic on a nil value (local 'saldo')'. ¿Por dónde empiezas?
Pista
Lee el error de izquierda a derecha: recurso → archivo:línea → qué pasó → variable.
Completa el print de depuración condicional para que solo salga cuando Config.Debug está activo.
Config.Debug = truelocal function dbg(...) if Config.Debug then ('[mi_banco]', ...) endendPista
La función básica de Lua para escribir en consola.
Ejecutas /resmon en la consola del cliente (F8). ¿Qué te dice y qué buscas?
Pista
res-mon = resource monitor. Mide rendimiento, no errores.
Challenge: code it yourself
A function isn't giving money and throws no error. Describe what prints you'd add to find where it breaks.
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 print on entry, one after GetPlayer (Player~=nil) and one with the amount.
Escribe aquí tu solución:
