Fundamentals: your first resource in QBCore · Lesson 5/5 · 8 min

Reading errors like a pro

Lua errors follow a pattern. Learn to read the stack trace and locate the cause in seconds.

Programming is, to a large extent, reading errors. The FiveM console (F8 on the client, the server console / txAdmin on the server) tells you exactly which file and which line failed.

The most common error: index a nil

text
SCRIPT ERROR: @taxi_job/server.lua:14: attempt to index a nil value (local 'Player')

A typical error

It reads like this: in server.lua, line 14, you tried to access something (.PlayerData, .Functions...) of a Player variable that was nil. Translation: Player doesn't exist. Common cause in QBCore: QBCore.Functions.GetPlayer(src) returned nil because the player isn't loaded yet or the ID is wrong.

  • attempt to index a nil value → you used something.field but "something" is nil (did GetPlayer return nil?).
  • attempt to call a nil value → you called a function that doesn't exist (typo? misspelled export?).
  • attempt to perform arithmetic on a nil value → you added/operated with a nil.
  • attempt to concatenate a nil value → you joined text with a nil (..).

Trick: after GetPlayer ALWAYS check if not Player then return end. Defensive programming avoids 80% of crashes.

In the Guides section you have solutions to the most frequent QBCore errors (including attempt to index a nil value). Use it as a dictionary when you get stuck.

Practice what you learned

0/3
Test

Ves este error: «attempt to index a nil value (local 'Player')». ¿Qué significa?

Corrige el error

El error «attempt to call a nil value (field 'AddMoneyy')» viene de un typo. Corrige el nombre del método.

Este código tiene un fallo:

1Player.Functions.AddMoneyy('cash', 500)

Reescríbelo corregido:

Pista

Mira bien el nombre del método... tiene una letra de más. Debería ser AddMoney.

Rellena los huecos

Completa el patrón defensivo que evita el 80% de los crashes tras obtener al jugador.

1local Player = QBCore.Functions.GetPlayer(source)
2if not Player then end
Pista

Si el jugador no existe, corta la ejecución de la función antes de tocar sus datos.

Challenge: code it yourself

You're given this error: attempt to call a nil value (field 'AddMoneyy'). What's the most likely cause and how would you fix it?

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

Look closely at the method name... is it spelled right? It should be AddMoney.

Escribe aquí tu solución:

How was this lesson?