Durability System

The Decay system will allow us to add temporary durability to items, just like a bad apple that cannot be used once it is broken. This system is completely optional and you can add it to certain items in your inventory using this documentation.

How to use in esx

To give temporary durability to an item, we will go to qs-inventory/shared/items.lua and add the following values to the specific item, you also have an explanation of what each value does on the item.

["created"] = nil, -- Required, but don't touch it, always nil!
["decay"] = 0.01, -- Item duration, less numbers, less duration.
["delete"] = false -- Do we remove the item when it breaks?

The item will be as seen below, which will now have temporary durability.

['tosti'] =  {
       ['name'] =  'tosti',
       ['label'] =  'Grilled Cheese Sandwich',
       ['weight'] =  200,
       ['type'] =  'item',
       ['image'] =  'tosti.png',
       ['unique'] =  false,
       ['useable'] =  true,
       ['shouldClose'] =  true,
       ['combinable'] =  nil,
       ['description'] =  'Nice to eat',
       ["created"] = nil, -- Required, but don't touch it, always nil!
       ["decay"] = 0.01, -- Item duration, less numbers, less duration.
       ["delete"] = false -- Do we remove the item when it breaks?
}
How to use qbcore

To give temporary durability to an item, we will go to qb-core/shared/items.lua and add the following values to the specific item, you also have an explanation of what each value does on the item.

-- created = Required, but don't touch it, always nil!
-- decay = Item duration, less numbers, less duration.
-- delete = Do we remove the item when it breaks?
["created"] = nil, ["decay"] = 0.01, ["delete"] = true

The item will be as seen below, which will now have temporary durability.

['sandwich'] = {['name'] = 'sandwich', ['label'] = 'Sandwich', ['weight'] = 200, ['type'] = 'item', ['image'] = 'sandwich.png', ['unique'] = false, ['useable'] = true, ['shouldClose'] = true,	['combinable'] = nil, ['description'] = 'Nice bread for your stomach', ["created"] = nil, ["decay"] = 0.01, ["delete"] = true},

Durability handling and uses

With the following snippet we will be able to deduct 10 durability from the next item for each use. Manage it as you want to create unique and exclusive items.

This small snippet will give you a demonstration of manipulation of the durability metadata, with this code we are using an item that breaks with each use. Normally we will not use qs-inventory exports, but for standalone or specific metadata codes they will be necessary.

exports['qs-inventory']:CreateUsableItem('firstaid', function(source)
    local item = exports['qs-inventory']:GetItemByName(source, 'firstaid')       
    if item.info.quality >= 10 then
        item.info.quality = item.info.quality - 10
        print("Item:", item.name, "Slot:", item.slot, "Info:", json.encode(item.info))
        exports['qs-inventory']:SetItemMetadata(source, item.slot, item.info)

        print("You used the item. Remaining durability:", item.info.quality)
    else
        print("The item is broken, you can't use it")
    end
end)

Example to obtain durability

You can use and play with certain events to execute this function, just like in the example above. In this case, for example, we create a command to verify the complete information of your item and its durability.

RegisterCommand('getdurability', function()
    local weapon = exports['qs-inventory']:GetCurrentWeapon()
    if not weapon then return end
    print('Weapon info:', json.encode(weapon, { indent = true }))
    local durability = weapon.info.quality
    print('Weapon durability:', durability)
end, false)

Last updated