qbcore-framework / qb-weapons

Weapon Logic Script For QB-Core :gun:
GNU General Public License v3.0
29 stars 194 forks source link

[BUG] guns not reloading #175

Closed 69Gamerss closed 1 year ago

69Gamerss commented 1 year ago

Describe the bug when i try to reload it shows bullets used but nothing happens

To Reproduce Steps to reproduce the behavior: get a gun get bullets use the bullets

Expected behavior reload the gun and remove the bullets

Screenshots image

Questions (please complete the following information):

69Gamerss commented 1 year ago

found a fix, the script was comparing hashes with name,

here is a fix

RegisterNetEvent('weapons:client:AddAmmo', function(type, amount, itemData)
    local ped = PlayerPedId()
    local weaponHash = GetSelectedPedWeapon(ped)
    local weaponName = nil

    for key, value in pairs(QBCore.Shared.Weapons) do
        local convertedHash = GetHashKey(key)
        if convertedHash == weaponHash then
            weaponName = key
            break
        end
    end

    if weaponName then
        if QBCore.Shared.Weapons[weaponName]["name"] ~= "weapon_unarmed" and QBCore.Shared.Weapons[weaponName]["ammotype"] == type:upper() then  
            local total = GetAmmoInPedWeapon(ped, weaponHash)
            local _, maxAmmo = GetMaxAmmo(ped, weaponHash)
            if total < maxAmmo then
                QBCore.Functions.Progressbar("taking_bullets", Lang:t('info.loading_bullets'), Config.ReloadTime, false, true, {
                    disableMovement = false,
                    disableCarMovement = false,
                    disableMouse = false,
                    disableCombat = true,
                }, {}, {}, {}, function() -- Done
                    if QBCore.Shared.Weapons[weaponName] then
                        AddAmmoToPed(ped,weaponName,amount)
                        TaskReloadWeapon(ped)
                        print(CurrentWeaponData,total,amount)
                        TriggerServerEvent("weapons:server:UpdateWeaponAmmo", CurrentWeaponData, total + amount)
                        TriggerServerEvent('weapons:server:removeWeaponAmmoItem', itemData)
                        TriggerEvent('inventory:client:ItemBox', QBCore.Shared.Items[itemData.name], "remove")
                        TriggerEvent('QBCore:Notify', Lang:t('success.reloaded'), "success")
                    end
                end, function()
                    QBCore.Functions.Notify(Lang:t('error.canceled'), "error")
                end)
            else
                QBCore.Functions.Notify(Lang:t('error.max_ammo'), "error")
            end
        else
            QBCore.Functions.Notify(Lang:t('error.no_weapon'), "error")
        end
    else
        QBCore.Functions.Notify(Lang:t('error.no_weapon'), "error")
    end
end)