๐Ÿ“ŠPlayer Data

Learn how to access and modify a player's data

QBCore.Player.Login

  • On player login, gets their data or sets default values

function QBCore.Player.Login(source, citizenid, newData)
    local src = source
    if src then
        if citizenid then
            local result = MySQL.query.await('SELECT * FROM players WHERE citizenid = ?', { citizenid })
            local PlayerData = result[1]
            if PlayerData then
                PlayerData.money = json.decode(PlayerData.money)
                PlayerData.job = json.decode(PlayerData.job)
                PlayerData.position = json.decode(PlayerData.position)
                PlayerData.metadata = json.decode(PlayerData.metadata)
                PlayerData.charinfo = json.decode(PlayerData.charinfo)
                if PlayerData.gang then
                    PlayerData.gang = json.decode(PlayerData.gang)
                else
                    PlayerData.gang = {}
                end
            end
            QBCore.Player.CheckPlayerData(src, PlayerData)
        else
            QBCore.Player.CheckPlayerData(src, newData)
        end
        return true
    else
        QBCore.ShowError(GetCurrentResourceName(), 'ERROR QBCORE.PLAYER.LOGIN - NO SOURCE GIVEN!')
        return false
    end
end

QBCore.Player.CheckPlayerData

  • Function called above on player join to gather player data (this is where you can add/remove additional player data)


QBCore.Player.Logout

  • Saves player on logout and removes them from active players table


QBCore.Player.CreatePlayer

  • Creates a new character and sets default data (any function inside the self table can be called on the player after using the GetPlayer function)


QBCore.Player.Save

  • Saves the player's info to the database


QBCore.Player.DeleteCharacter

  • Deletes a character and all corresponding data in the database

Last updated