Skip to Content
Cobalt

Hooks and events

Run code before inventory actions, and react to items being used, weapons being drawn and pockets loading.

Hooks

RegisterHook(event, fn, priority?, after?) runs your code before addItem, removeItem, moveItem, useItem, openInventory, buyItem, craftItem or openShop. Return false to cancel the action.

  • Order: lower priority runs first; the default is 50.
  • After the action: after(success, payload) runs 50 ms after it ends.
  • Removing a hook: the call returns a hook id for RemoveHook. All of a resource's hooks go when that resource stops.
exports.cobalt_inventory:RegisterHook('moveItem', function(payload)
    if payload.toInv:find('^trunk%-') and payload.item.name == 'markedbills' then
        return false
    end
end)

Pockets loaded

A player's pockets load shortly after cobalt_core:Server:PlayerLoaded. Once they're loaded, cobalt_inventory sets the server-only state Player(source).state.inventoryLoaded to true. A resource that gives items at login should wait for it first:

AddEventHandler('cobalt_core:Server:PlayerLoaded', function(player)
    local source = player.PlayerData.source

    lib.waitFor(function()
        return Player(source).state.inventoryLoaded or nil
    end, 'pockets did not load', 10000)

    exports.cobalt_inventory:AddItem(source, 'phone', 1)
end)

Client events

AddEventHandler('cobalt_inventory:client:itemUsed', function(itemName, slot)
    print(itemName, slot.metadata)
end)

AddEventHandler('cobalt_inventory:client:currentWeapon', function(weapon)
    print(weapon and weapon.name, weapon and weapon.metadata.durability)
end)

AddEventHandler('cobalt_inventory:client:shopPedSpawned', function(shopId, locationIndex, ped)
    print(shopId, locationIndex, ped)
end)
EventFires
cobalt_inventory:client:itemUsedAfter the local player uses an item, with its name and the stack
cobalt_inventory:client:currentWeaponWhen a weapon is drawn, holstered or broken, and when its metadata changes. The weapon is { name, invName, itemId, metadata }, or nil when unarmed
cobalt_inventory:client:shopPedSpawnedEach time a shopkeeper ped spawns at a counter

The server's GetCurrentWeapon(source) export and the client's GetCurrentWeapon() return the held weapon in the same { name, invName, itemId, metadata } shape.

On this page