AWS DEVELOPMENT / DOCUMENTATION

Your server. Our expertise.

Everything you need to install, configure and build.

94 guides • 11 resources • Always here to help
About resource versions

These guides include the original resource names. Use the folder and SQL names supplied with your downloaded version. A renamed AWS package may differ from the original EXIT release.

esx.lua

function DependencyCheck(data)
    for k, v in pairs(data) do
        if GetResourceState(k):find('started') ~= nil then
            return v
        end
    end
    return false
end

local frameworks = {
    ['es_extended'] = 'esx',
    ['qb-core'] = 'qb',
    ['qbx_core'] = 'qb'
}

Config.Framework = DependencyCheck(frameworks) or 'none'


if Config.Framework ~= "esx" then
    return
end

Citizen.CreateThreadNow(function()
    esx = exports["es_extended"]:getSharedObject()
end)

RegisterNetEvent('esx:playerLoaded', function(id, data)
    Wait(2000)
    -- print('Loaded player:', id)
    CreateQuests(id)
end)

CreateThread(function()
    for k, v in pairs(esx.Players) do
        if v and v.source then
            -- print('Loaded player:', v.source)
            CreateQuests(v.source)
        end
    end
end)

function GetPlayeridentifier(source)
    local Player = GetPlayerId(source)
    if Player then
        return Player.identifier
    end
end

function GetPlayerId(source)
    local Player = esx.GetPlayerFromId(source)
    return Player
end

function GetItemCountFromInventory(src, item)
    if not src then src = source end
    local xPlayer = esx.GetPlayerFromId(src)
    if not xPlayer then return 0 end

    local function normalize(str)
        if not str then return "" end
        return str:lower():gsub("%s+", ""):gsub("_", ""):gsub("[^%w]", "")
    end

    local target = normalize(item)
    local count = 0

    -- 1. Try direct exports for best accuracy (if using qs/ox on ESX)
    if GetResourceState('exit-inventory') == 'started' then
        count = exports['exit-inventory']:GetItemCount(src, item)
    elseif GetResourceState('ox_inventory') == 'started' then
        count = exports.ox_inventory:GetItemCount(src, item)
    end

    -- 2. ESX Default Inventory Scan
    if count == 0 and xPlayer.inventory then
        for _, invItem in pairs(xPlayer.inventory) do
            if invItem and (invItem.name or invItem.label) then
                local normName = normalize(invItem.name)
                local normLabel = normalize(invItem.label)
                if normName == target or normLabel == target or string.find(normName, target) then
                    count = count + (invItem.count or invItem.amount or 0)
                end
            end
        end
    end

    if count > 0 then
        -- print(string.format("^2[Lumberjack Debug ESX]^7 Found %d x %s for Player %s (%s)", count, item, GetPlayerName(src), src))
    end
    
    return count
end

function AddInventoryItem(src, item, quantity)
    local xPlayer = esx.GetPlayerFromId(src)
    if xPlayer.canCarryItem(item, quantity) then
        xPlayer.addInventoryItem(item, quantity)
    else
        return false
    end
end

function RemoveInventoryItem(item, quantity)
    local src = source
    if not src or src == 0 then return end
    local xPlayer = esx.GetPlayerFromId(src)
    if not xPlayer then return end

    -- print(string.format("^3[Lumberjack Debug ESX]^7 Removing %d x %s from %s", quantity, item, GetPlayerName(src)))

    if GetResourceState('exit-inventory') == 'started' then
        exports['exit-inventory']:RemoveItem(src, item, quantity)
    elseif GetResourceState('ox_inventory') == 'started' then
        exports.ox_inventory:RemoveItem(src, item, quantity, nil)
    else
        xPlayer.removeInventoryItem(item, quantity)
    end
end

function AddMoney(source, price)
    local xPlayer = GetPlayerId(source)
    xPlayer.addAccountMoney('money', price)
end

function RemoveMoney(price, src)
    if src then
        local xPlayer = GetPlayerId(src)
        xPlayer.removeAccountMoney('money', price)
    else
        local xPlayer = GetPlayerId(source)
        xPlayer.removeAccountMoney('money', price)
    end
end

function GetMoney(source)
    local xPlayer = GetPlayerId(source)
    local money = xPlayer.getMoney()
    return money
end

function RegisterServerCallback(name, cb)
    esx.RegisterServerCallback(name, cb)
end

function GetPlayers()
    local xPlayers = esx.GetPlayers()
    return xPlayers
end

function GetPlayerFromIdentifier(identifier)
    local xPlayer = esx.GetPlayerFromIdentifier(identifier)
    if xPlayer then
        return xPlayer.source
    else
        return nil
    end
end