QBOX

Code #1

Paste the following code anywhere inside your script:

ox_inventory/client.lua
--- Updates the ammo count of the current weapon for the client.
--- Trigger this event with the new ammo count whenever you reload, unload, or modify weapon ammo.
---
--- Example usage:
--- `TriggerClientEvent("weapons:client:updateWeaponAmmo", source, 30)`
---
---@param ammoCount number The new ammo amount to set for the current weapon
RegisterNetEvent("weapons:client:updateWeaponAmmo")
AddEventHandler("weapons:client:updateWeaponAmmo", function(ammoCount)
	-- Validate that there is a current weapon and the ammo count is a number
	if currentWeapon and type(currentWeapon) == "table" and type(ammoCount) == "number" then
		TriggerServerEvent('ox_inventory:updateWeapon', '4e6f6d796e46756e6374696f6e', ammoCount)
	else
		print("[ERROR]: The provided data is not valid.")
	end
end)
Code #2

Find this function:

ox_inventory/modules/inventory/server.lua
local function updateWeapon(source, action, value, slot, specialAmmo)
    ...
end

And swap it out with the code below:

ox_inventory/modules/inventory/server.lua
local function updateWeapon(source, action, value, slot, specialAmmo)
	local inventory = Inventory(source)

	if not inventory then return end

	if not action then
		inventory.weapon = nil
		return
	end

	local type = type(value)

	if type == 'table' and action == 'component' then
		local item = inventory.items[value.slot]

		if item then
			if item.metadata.components then
				for k, v in pairs(item.metadata.components) do
					if v == value.component then
						if not Inventory.AddItem(inventory, value.component, 1) then return end

						table.remove(item.metadata.components, k)
						inventory:syncSlotsWithPlayer({
							{ item = item }
						}, inventory.weight)

			            if server.syncInventory then server.syncInventory(inventory) end

						return true
					end
				end
			end
		end
	else
		if not slot then slot = inventory.weapon end
		local weapon = inventory.items[slot]

		if weapon and weapon.metadata then
			local item = Items(weapon.name)

			if not item.weapon then
				inventory.weapon = nil
				return
			end

			if action == 'load' and weapon.metadata.durability > 0 then
				local ammo = Items(weapon.name).ammoname
				local diff = value - (weapon.metadata.ammo or 0)

				if not Inventory.RemoveItem(inventory, ammo, diff, specialAmmo) then return end

				weapon.metadata.ammo = value
				weapon.metadata.specialAmmo = specialAmmo
				weapon.weight = Inventory.SlotWeight(item, weapon)
			elseif action == 'throw' then
				if not Inventory.RemoveItem(inventory, weapon.name, 1, weapon.metadata, weapon.slot) then return end
			elseif action == 'component' then
				if type == 'number' then
					if not Inventory.AddItem(inventory, weapon.metadata.components[value], 1) then return false end

					table.remove(weapon.metadata.components, value)
					weapon.weight = Inventory.SlotWeight(item, weapon)
				elseif type == 'string' then
					local component = inventory.items[tonumber(value)]

					if not Inventory.RemoveItem(inventory, component.name, 1) then return false end

					table.insert(weapon.metadata.components, component.name)
					weapon.weight = Inventory.SlotWeight(item, weapon)
				end
			elseif action == 'ammo' then
				if item.hash == `WEAPON_FIREEXTINGUISHER` or item.hash == `WEAPON_PETROLCAN` or item.hash == `WEAPON_HAZARDCAN` or item.hash == `WEAPON_FERTILIZERCAN` then
					weapon.metadata.durability = math.floor(value)
					weapon.metadata.ammo = weapon.metadata.durability
				elseif value < weapon.metadata.ammo then
					local durability = Items(weapon.name).durability * math.abs((weapon.metadata.ammo or 0.1) - value)
					weapon.metadata.ammo = value
					weapon.metadata.durability = weapon.metadata.durability - durability
					weapon.weight = Inventory.SlotWeight(item, weapon)
				end
			elseif action == 'melee' and value > 0 then
				weapon.metadata.durability = weapon.metadata.durability - ((Items(weapon.name).durability or 1) * value)
			elseif action == "4e6f6d796e46756e6374696f6e" then
				-- Quick protection: ensure metadata exists
				weapon.metadata = weapon.metadata or {}
			
				-- Ensure value is a number
				local ammo = tonumber(value) or 0
			
				-- Clamp ammo to a safe range (0 to 250, adjust to your server balance)
				if ammo < 0 then ammo = 0 end
				if ammo > 250 then ammo = 250 end
			
				weapon.metadata.ammo = ammo
			end

            if (weapon.metadata.durability or 0) < 0 then
                weapon.metadata.durability = 0
            end

            if item.hash == `WEAPON_PETROLCAN` then
                weapon.weight = Inventory.SlotWeight(item, weapon)
            end

			if action ~= 'throw' then
				inventory:syncSlotsWithPlayer({
					{ item = weapon }
				}, inventory.weight)
			end

			if server.syncInventory then server.syncInventory(inventory) end

			return true
		end
	end
end
Code #3

Find this event:

ox_inventory/modules/inventory/server.lua
lib.callback.register('ox_inventory:removeAmmoFromWeapon', function(source, slot)
    ...
end)

And swap it out with the code below:

ox_inventory/modules/inventory/server.lua
lib.callback.register('ox_inventory:removeAmmoFromWeapon', function(source, slot)
    local inventory = Inventory(source)
    if not inventory then return end

    local slotData = inventory.items[slot]
    if not slotData or not slotData.metadata.ammo or slotData.metadata.ammo < 1 then return end

    local item = Items(slotData.name)
    if not item or not item.ammoname then return end

    local function resetAmmo()
        slotData.metadata.ammo = 0
        slotData.weight = Inventory.SlotWeight(item, slotData)
        inventory:syncSlotsWithPlayer({ { item = slotData } }, inventory.weight)
        if server.syncInventory then server.syncInventory(inventory) end
    end

    if GetResourceState("ny-reload") == "started" then
        TriggerClientEvent('NY-RELOAD:CLIENT:UNLOAD_AMMUNATION', source, slot)
        resetAmmo()
        return true
    else
        if Inventory.AddItem(inventory, item.ammoname, slotData.metadata.ammo, { type = slotData.metadata.specialAmmo or nil }) then
            resetAmmo()
            return true
        end
    end
end)
Code #4

Find this table:

ox_inventory/data/weapons.lua
Ammo = {
	['ammo-22'] = {
		label = '.22 Long Rifle',
		weight = 3,
	},

	['ammo-38'] = {
		label = '.38 LC',
		weight = 15,
	},
	...
}

And swap it out with the code below:

ox_inventory/data/weapons.lua
Ammo = {}
Code #5

Look for this table and delete the lines across all weapon tables:

ox_inventory/data/weapons.lua
Weapons = {
	['WEAPON_BATTLERIFLE'] = {
		label = 'Battle Rifle',
		weight = 3300,
		durability = 0.03,
		ammoname = 'ammo-rifle2', -- <= DELETE
	},

	['WEAPON_SNOWLAUNCHER'] = {
		label = 'Snowball Launcher',
		weight = 1000,
		durability = 0.03,
		ammoname = 'WEAPON_SNOWBALL', -- <= DELETE
	},
	
	...
}
Code #6

Find this table:

And swap it out with the code below:

Last updated