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: "motorcycle_license", "car_license", "truck_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 385 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 == 'A' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameMoto,
                ['@owner'] = xPlayer.identifier
            })
        elseif license == 'B' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameCar,
                ['@owner'] = xPlayer.identifier
            })
        elseif license == 'C' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameTruck,
                ['@owner'] = xPlayer.identifier
            })
        end
    elseif Config.Framework == "qb" then
        if license == 'A' then
            local licenseTable = xPlayer.PlayerData.metadata['licences']
            licenseTable[Config.licenseNameMoto] = true
            xPlayer.Functions.SetMetaData('licences', licenseTable)
        elseif license == 'B' then
            local licenseTable = xPlayer.PlayerData.metadata['licences']
            licenseTable[Config.licenseNameCar] = true
            xPlayer.Functions.SetMetaData('licences', licenseTable)
        elseif license == 'C' then
            local licenseTable = xPlayer.PlayerData.metadata['licences']
            licenseTable[Config.licenseNameTruck] = 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.firstname .. ' ' .. xPlayer.PlayerData.lastname
info.citizenid = xPlayer.PlayerData.citizenid
xPlayer.Functions.AddItem('motorcycle_license', 1, nil, info)

the code would look like this:

...
elseif Config.Framework == "qb" then
        if license == 'A' then
            local licenseTable = xPlayer.PlayerData.metadata['licences']
            licenseTable[Config.licenseNameMoto] = 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('motorcycle_license', 1, nil, info)
        elseif license == 'B' then
            local licenseTable = xPlayer.PlayerData.metadata['licences']
            licenseTable[Config.licenseNameCar] = 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('car_license', 1, nil, info)
        elseif license == 'C' then
            local licenseTable = xPlayer.PlayerData.metadata['licences']
            licenseTable[Config.licenseNameTruck] = 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('truck_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 == 'A' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameMoto,
                ['@owner'] = xPlayer.identifier
            })
            TriggerEvent("licenses:addLicense", xPlayer.source, "motorcycle")
        elseif license == 'B' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameCar,
                ['@owner'] = xPlayer.identifier
            })
            TriggerEvent("licenses:addLicense", xPlayer.source, "car")
        elseif license == 'C' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameTruck,
                ['@owner'] = xPlayer.identifier
            })
            TriggerEvent("licenses:addLicense", xPlayer.source, "truck")
        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.." ".. "motorcycle")

the code would look like this:

...
    if Config.Framework == "esx" then
        if license == 'A' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameMoto,
                ['@owner'] = xPlayer.identifier
            })
            ExecuteCommand("addlicense ".." "..xPlayer.source.." ".. "motorcycle")
        elseif license == 'B' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameCar,
                ['@owner'] = xPlayer.identifier
            })
            ExecuteCommand("addlicense ".." "..xPlayer.source.." ".. "car")
        elseif license == 'C' then
            SqlFunc(Config.Mysql, 'execute', 'INSERT INTO user_licenses (type, owner) VALUES (@type, @owner)', {
                ['@type'] = Config.licenseNameTruck,
                ['@owner'] = xPlayer.identifier
            })
            ExecuteCommand("addlicense ".." "..xPlayer.source.." ".. "truck")
        end
...

Last updated