License Item

By default, the license is applied to the database (both on ESX and QB). If what you want is to perform a license delivery as an item you must take into account if the license is simply an item that you have created in your core. For example, something like: "boat_license", "yacht_license". Or if it is managed by a license script.

ITEMS CREATED IN THE CORE

If you have created the item from the core, as if it were any other type of item and your core allows you to have metadata in the items you can do the following assuming that you have the items indicated above In line 362 of config.lua you have an open function called "applyLicense" in which you can edit what happens when a user successfully completes the practical test.

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

In this example we will use the QB version to add an item to the user, according to the test he has performed. We will use the following code to deliver the item with the metadata of the name and citizenid. This way each license delivered will be unique:

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)

the code would look like this:

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

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, "boat")
    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, "yacht")
    end
[...]

BY COMMAND

As a third option, although we do not recommend it. It is possible to deliver licenses if your licensing script allows you to deliver a license to a user by command.

For example, if the script has the command /addlicense ID Type

You could use:

ExecuteCommand("addlicense ".." "..xPlayer.source.." ".. "helicopter")

the code 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
        })
        ExecuteCommand("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
        })
        ExecuteCommand("addlicense ".." "..xPlayer.source.." ".. "plane")
    end
[...]

Last updated