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.

text
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)

lua
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

text
# 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/3
Test

Lees 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.

Rellena los huecos

Completa el print de depuración condicional para que solo salga cuando Config.Debug está activo.

1Config.Debug = true
2local function dbg(...)
3 if Config.Debug then ('[mi_banco]', ...) end
4end
Pista

La función básica de Lua para escribir en consola.

Test

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:

How was this lesson?