πŸ”₯All scripts on our website are 50% off!πŸ”₯
Logo

License Item

By default, the license is applied to the database (both on ESX and QB). If you want to deliver a license as an item, you can customize the applyLicense function.

Items Created in the Core

If you have created the item from the core (e.g., helicopter_license, plane_license) and your core allows metadata in items, you can modify the function in line 362 of config.lua.

Default Function

function applyLicense(xPlayer, license)
    if Config.Framework == "esx" then
        if license == 'HPL' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameHelicopter,
                ['@owner'] = xPlayer.identifier
            })
        elseif license == 'PPL' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNamePlane,
                ['@owner'] = xPlayer.identifier
            })
        end
    elseif Config.Framework == "qb" then
        if license == 'HPL' then
            local licenseTable = xPlayer.PlayerData.metadata['licences']
            licenseTable[Config.licenseNameHelicopter] = true
            xPlayer.Functions.SetMetaData('licences', licenseTable)
        elseif license == 'PPL' then
            local licenseTable = xPlayer.PlayerData.metadata['licences']
            licenseTable[Config.licenseNamePlane] = true
            xPlayer.Functions.SetMetaData('licences', licenseTable)
        end
    end
end

Adding Item Delivery (QBCore Example)

To deliver the license as an item with metadata (player name and citizen ID):

local info = {}
info.playerName = xPlayer.PlayerData.charinfo.firstname .. ' ' .. xPlayer.PlayerData.charinfo.lastname
info.citizenid = xPlayer.PlayerData.citizenid
xPlayer.Functions.AddItem('helicopter_license', 1, nil, info)

Complete Example

elseif Config.Framework == "qb" then
    if license == 'HPL' then
        local licenseTable = xPlayer.PlayerData.metadata['licences']
        licenseTable[Config.licenseNameHelicopter] = true
        xPlayer.Functions.SetMetaData('licences', licenseTable)
        local info = {}
        info.playerName = xPlayer.PlayerData.charinfo.firstname .. ' ' .. xPlayer.PlayerData.charinfo.lastname
        info.citizenid = xPlayer.PlayerData.citizenid
        xPlayer.Functions.AddItem('helicopter_license', 1, nil, info)
    elseif license == 'PPL' then
        local licenseTable = xPlayer.PlayerData.metadata['licences']
        licenseTable[Config.licenseNamePlane] = true
        xPlayer.Functions.SetMetaData('licences', licenseTable)
        local info = {}
        info.playerName = xPlayer.PlayerData.charinfo.firstname .. ' ' .. xPlayer.PlayerData.charinfo.lastname
        info.citizenid = xPlayer.PlayerData.citizenid
        xPlayer.Functions.AddItem('plane_license', 1, nil, info)
    end
end

ESX Example

For ESX, you can use a similar approach:

if Config.Framework == "esx" then
    if license == 'HPL' then
        SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
            ['@type'] = Config.licenseNameHelicopter,
            ['@owner'] = xPlayer.identifier
        })
        xPlayer.addInventoryItem('helicopter_license', 1)
    elseif license == 'PPL' then
        SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
            ['@type'] = Config.licenseNamePlane,
            ['@owner'] = xPlayer.identifier
        })
        xPlayer.addInventoryItem('plane_license', 1)
    end
end

Items Generated by License Script

If instead of creating the license items manually, you are using a license script, it may be easier to adapt the code. You will have to check the documentation of your license script but usually you will be given an export or trigger that you can run in any function.

Imagine that we have this trigger obtained from the licensing script documentation:

TriggerServerEvent("licenses:addLicense", PlayerID, LicenseType)

An example, in this case on the ESX side would look like this:

if Config.Framework == "esx" then
    if license == 'HPL' then
        SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
            ['@type'] = Config.licenseNameHelicopter,
            ['@owner'] = xPlayer.identifier
        })
        TriggerEvent("licenses:addLicense", xPlayer.source, "helicopter")
    elseif license == 'PPL' then
        SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
            ['@type'] = Config.licenseNamePlane,
            ['@owner'] = xPlayer.identifier
        })
        TriggerEvent("licenses:addLicense", xPlayer.source, "plane")
    end
end

Creating License Items

Don't forget to create the items in your inventory system:

QBCore (qb-core/shared/items.lua)

['helicopter_license'] = {
    ['name'] = 'helicopter_license',
    ['label'] = 'Helicopter Pilot License',
    ['weight'] = 0,
    ['type'] = 'item',
    ['image'] = 'helicopter_license.png',
    ['unique'] = true,
    ['useable'] = false,
    ['shouldClose'] = false,
    ['combinable'] = nil,
    ['description'] = 'HPL - Helicopter Pilot License'
},
['plane_license'] = {
    ['name'] = 'plane_license',
    ['label'] = 'Private Pilot License',
    ['weight'] = 0,
    ['type'] = 'item',
    ['image'] = 'plane_license.png',
    ['unique'] = true,
    ['useable'] = false,
    ['shouldClose'] = false,
    ['combinable'] = nil,
    ['description'] = 'PPL - Private Pilot License'
}

ESX (es_extended/config.lua or database)

INSERT INTO `items` (`name`, `label`, `weight`, `rare`, `can_remove`) VALUES
('helicopter_license', 'Helicopter Pilot License', 1, 0, 1),
('plane_license', 'Private Pilot License', 1, 0, 1);

Notes

  • Each license delivered will be unique with metadata
  • You can check licenses using the metadata system
  • The license is stored both in the database and as an item
  • Players can show their license to other players
  • You can add custom images for the license items