Useable Items

If you want to add throwing feature to usable items, you should follow this step

Step 1: Find the code of the item used on the server side

In this example I will show qb-smallresources/server/consumables.lua which is available in qb-core

for k, _ in pairs(Config.Consumables.eat) do
    QBCore.Functions.CreateUseableItem(k, function(source, item)
        local Player = QBCore.Functions.GetPlayer(source)
        if not Player.Functions.RemoveItem(item.name, 1, item.slot) then return end
        TriggerClientEvent('consumables:client:Eat', source, item.name)
    end)
end

When item is used, this event consumables:client:Eat is triggered on the client side and the name of the item goes client side.

Step 2: Modify Event

After finding the event going to the client, you should add the export below to the head of the event.

if not exports["rm_throw"]:useableItem(itemName) then
    return
end

Example:

RegisterNetEvent('consumables:client:Eat', function(itemName)
    if not exports["rm_throw"]:useableItem(itemName) then
        return
    end
    QBCore.Functions.Progressbar('eat_something', Lang:t('consumables.eat_progress'), 5000, false, true, {
        disableMovement = false,
        disableCarMovement = false,
        disableMouse = false,
        disableCombat = true
    }, {
        animDict = 'mp_player_inteat@burger',
        anim = 'mp_player_int_eat_burger',
        flags = 49
    }, {
        model = 'prop_cs_burger_01',
        bone = 60309,
        coords = vec3(0.0, 0.0, -0.02),
        rotation = vec3(30, 0.0, 0.0),
    }, {}, function() -- Done
        TriggerEvent('inventory:client:ItemBox', QBCore.Shared.Items[itemName], 'remove')
        TriggerServerEvent('consumables:server:addHunger', QBCore.Functions.GetPlayerData().metadata.hunger + Config.Consumables.eat[itemName])
        TriggerServerEvent('hud:server:RelieveStress', math.random(2, 4))
    end)
end)

Last updated