Need help making oxmysql custom id system inspired by vrp

Im trying to make a id system where the first player who joins get id 1, and the second player who joins get id 2, and so on.

i have managed to setup my database, with a table named users and columns named ID (is the primary key), SteamID, Licence, XBL, DiscordID, LiveID, IP and CreatedDate.

The ID colum is auto increment, so it does the id stuff automatically when i add a user to the database.

But i have some issues where i can create the user and all that stuff, i can also get the user_id from the server site, but not from the client site.

Here is my server site code:

local function GetUserLicence()
    local license  = false
    local lic 

    print(" ")
    print('GetUserIdentifier EVENT running')
    print(" ")

    for k,v in pairs(GetPlayerIdentifiers(source))do
        if string.sub(v, 1, string.len("license:")) == "license:" then
            --print(v)
            license = v
            lic = v
            --print(lic)
        end

    end

    print('Server Side LIC is:')
    print(lic)

    return lic
end

RegisterNetEvent('GetUserLicence')
AddEventHandler('GetUserLicence', GetUserLicence)

local function OnPlayerConnecting(name, setKickReason, deferrals)
    print("Player joined")
    local player = source
    local steamid  = false
    local license  = false
    local discord  = false
    local xbl      = false
    local liveid   = false
    local ip       = false

    for k,v in pairs(GetPlayerIdentifiers(source))do
        print(v)
        
        if string.sub(v, 1, string.len("steam:")) == "steam:" then
            steamid = v
        elseif string.sub(v, 1, string.len("license:")) == "license:" then
            license = v
        elseif string.sub(v, 1, string.len("xbl:")) == "xbl:" then
            xbl  = v
        elseif string.sub(v, 1, string.len("ip:")) == "ip:" then
            ip = v
        elseif string.sub(v, 1, string.len("discord:")) == "discord:" then
            discord = v
        elseif string.sub(v, 1, string.len("live:")) == "live:" then
            liveid = v
        end
    end

    MySQL.prepare('SELECT  `ID` FROM `users` WHERE `Licence` = ?', {license}, function(result)
        print(json.encode(result))
        if result then
            -- user data was here 
            print("user was already created")
            print(json.encode(result))
            user_id = json.encode(result)
            print("ID:")
            print(user_id)
            
        else
            -- insert user data here
            MySQL.insert('INSERT INTO users (SteamID, Licence, XBL, DiscordID, LiveID, IP) values(?, ?, ?, ?, ?, ?)',{steamid, license, xbl, discord, liveid, ip}, function(result)
                if(result) then 
                    print("user wasnt created")
                    MySQL.prepare('SELECT `ID` FROM `users` WHERE `Licence` = ?', {license}, function(result)
                        print(json.encode(result))
                        user_id = result
                        print(user_id)
                    end)
                end

            end)
        end 
    end)

    -- mandatory wait!
    Wait(0)

end

AddEventHandler("playerConnecting", OnPlayerConnecting)

local function GetUserID()
    print('Server GetUserID is running')
    local player = source
    local LicenceID = GetUserLicence()
    local id = -1

    MySQL.prepare('SELECT  `ID` FROM `users` WHERE `Licence` = ?', {LicenceID}, function(result)
        if result then
            id = result
        end
    end)
    return result
end

exports('sGetUserID', GetUserID)

local function GetIDFromDatabase(LicenceID)
    print('LicenceID is')
    print(LicenceID)
    MySQL.prepare('SELECT  `ID` FROM `users` WHERE `Licence` = ?', {LicenceID}, function(result)
        return result
    end)
end

RegisterNetEvent('GetIDFromDatabase')

AddEventHandler('GetIDFromDatabase', GetIDFromDatabase)

And here is my client site code:

local function GetUserLicence()
    local lic = TriggerServerEvent('GetUserLicence')
    print('Lic is:')
    print(lic)
    return lic
end

local function GetUserID()
    print('Client GetUserID is running')
    local player = source
    local LicenceID = GetUserLicence()
    local id = -1

    id = TriggerServerEvent('GetIDFromDatabase', LicenceID)
    print(id)
    return id


end

exports('cGetUserID', GetUserID)

i have another script where i have a command i can run and get my id printed in the client console, here is the code for that:

RegisterCommand('id', function(source, args)
    
    local id = exports['g-users']:cGetUserID()
    print("ID is: "..id)
    
end, false)

I dont know why but when i type /id in the chat i can see that the licence that the client site gets is false, and because of that my print gets an error because i cant concatenate a boolean value with a string in a print

I hope i can get some help

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.