1ESX

1) Please navigate to the file player.lua located in the directory es_extended/server/classes/. In this file, you will find a function named self.hasWeapon. You are to replace the entire content of this function, from the beginning to the end, with the new code provided.

Explanation:

  • This code is designed to prevent the loss of ammunition by restricting the use of more than one weapon of the same type. This implies that you will only be able to use one weapon per category, unless you dismantle the weapon in your inventory.

chevron-rightCodehashtag
es_extended/server/classes/player.lua
function self.hasWeapon(weaponName)
    -- Avoid making any changes if you're uncertain about your actions.

        local weaponHashes = {}

        for _, weaponData in ipairs(self.loadout) do
            local _, weapon = ESX.GetWeapon(weaponData.name)

            weaponHashes[weapon.ammo.hash] = true
        end

        local _, weaponToCheck = ESX.GetWeapon(weaponName)

        return weaponHashes[weaponToCheck.ammo.hash] or false
    end

2) Next, please navigate to the main.lua file located in the es_extended/client/ directory. At the end of this file, append the provided code.

Explanation:

  • This code is specifically engineered to store the updated ammunition count of your weapon within the metadata of your framework.

chevron-rightCodehashtag
es_extended/client/main.lua
RegisterNetEvent("weapons:client:updateWeaponAmmo")
AddEventHandler("weapons:client:updateWeaponAmmo", function(ammoCount)
    -- Avoid making any changes if you're uncertain about your actions.

    if ESX and ESX.PlayerLoaded and type(ammoCount) == "number" then
        local weaponHash = GetSelectedPedWeapon(ESX.PlayerData.ped)

        if weaponHash and type(weaponHash) == "number" then
            local weapon = ESX.GetWeaponFromHash(weaponHash)

            if weapon and type(weapon) == "table" then
                TriggerServerEvent("esx:updateWeaponAmmo", weapon.name, ammoCount)
            end
        end
    end
end)

Last updated