QBCore 'attempt to index a nil value' – Causes & Fix
The error
SCRIPT ERROR: @my-resource/server/main.lua:12: attempt to index a nil value (local 'Player')
What it means
Lua throws this when you use . or : on something that is nil. In QBCore it almost always means QBCore.Functions.GetPlayer(source) returned nil (wrong source, player not loaded yet, or you called it after they dropped), or QBCore itself was never set because you forgot GetCoreObject().
The fix
local QBCore = exports['qb-core']:GetCoreObject()
RegisterNetEvent('my-resource:server:giveCash', function(amount)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
if not Player then return end -- player not loaded / invalid source
amount = tonumber(amount)
if not amount or amount <= 0 then return end
Player.Functions.AddMoney('cash', amount, 'my-resource')
end)Always capture source into a local right away, and check the result of GetPlayer before using it. On the client, wait for QBCore:Client:OnPlayerLoaded before reading PlayerData.
Related tools: QBCore script generator · Fix a FiveM Lua error · all script generators