Source is null after MySQL query

So I’m building some kind of UUID system that gives everyone a unique ID the first time they join the server. I’m trying to display this UUID when a player joins. For that, I’ve been using the playerJoining event that also passes the source. After playing around for some while, I found out that before the MySQL query to get the UUID, the source works just fine but afterwards, source is nil. Here’s my code:

AddEventHandler('playerJoining', function(oldID)
    local static
    print('BEFORE: ' .. source .. ' | ' .. getLicense(source))
    local query = MySQL.single.await('SELECT * FROM staticid WHERE license = ?', {getLicense(source)})
    Citizen.Wait(500)
    static = query.staticid
    print('AFTER: ' .. source .. ' | ' .. getLicense(source) .. ' | ' .. static)
end)

Everything in the “BEFORE” print works just fine, including the getLicense function. But the “AFTER”-print throws following error:

[    script:utilities] SCRIPT ERROR: Execution of native 00000000ff7f66ab in script host failed: Argument at index 0 was null.
[    script:utilities] > GetNumPlayerIdentifiers (GetNumPlayerIdentifiers.lua:2)
[    script:utilities] > getLicense (@utilities/server.lua:383)
[    script:utilities] > handler (@utilities/server.lua:464)

This is my getLicense function (which works fine in any command etc.):

function getLicense(source)
    local _ids = GetPlayerIdentifiers(source)
    for _, v in pairs(_ids) do
        if string.find(v, "license") then
            license = v
            break
        end
    end
    return license
end

My database is alright as well as I tested that already and it returns the correct UUID.
I just don’t understand it. I also tried it without the Wait and with a normal callback, not a promise.

Edit: Here a shortened version without even using the UUID:

AddEventHandler('playerJoining', function(oldID)
    print('BEFORE: ' .. source .. ' | ' .. getLicense(source))
    local query = MySQL.single.await('SELECT * FROM staticid WHERE license = ?', {getLicense(source)})
    print('AFTER: ' .. source .. ' | ' .. getLicense(source))
end)

… throwing error:

[    script:utilities] SCRIPT ERROR: Execution of native 00000000ff7f66ab in script host failed: Argument at index 0 was null.
[    script:utilities] > GetNumPlayerIdentifiers (GetNumPlayerIdentifiers.lua:2)
[    script:utilities] > getLicense (@utilities/server.lua:383)
[    script:utilities] > handler (@utilities/server.lua:460)
[    script:utilities] > <unknown> (@oxmysql/dist/build.js:22252)
[    script:utilities] > processTicksAndRejections (node:internal/process/task_queues:96)

Fixed. Just created a variable src = source before mysql query lol.

1 Like

source is a global variable, not passed by playerJoined, so it changes all the time.
To fix that, just save source to local variable.

You will need to do this to all event handlers on the server-side, btw.

1 Like