Skip to content
This repository was archived by the owner on Apr 19, 2025. It is now read-only.

Commit 4819a3a

Browse files
committed
fix: weapon ammo update triggering twice
No longer handle currentWeapon as an event for locally use; set it separately as item or nil. Ensure currentWeapon.timer does not set to 0, just to set back to game timer.
1 parent afd95e7 commit 4819a3a

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

client.lua

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ local Utils = client.utils
55
local currentWeapon
66

77
RegisterNetEvent('ox_inventory:disarm', function(newSlot)
8-
Utils.Disarm(currentWeapon, newSlot)
8+
currentWeapon = Utils.Disarm(currentWeapon, newSlot)
99
end)
1010

1111
RegisterNetEvent('ox_inventory:clearWeapons', function()
1212
Utils.ClearWeapons(currentWeapon)
1313
end)
1414

15-
AddEventHandler('ox_inventory:currentWeapon', function(weapon)
16-
currentWeapon = weapon
17-
end)
18-
1915
local StashTarget
2016
exports('setStashTarget', function(id, owner)
2117
StashTarget = id and {id=id, owner=owner}
@@ -232,13 +228,14 @@ local function useSlot(slot)
232228
useItem(data, function(result)
233229
if result then
234230
if currentWeapon?.slot == result.slot then
235-
return Utils.Disarm(currentWeapon)
231+
currentWeapon = Utils.Disarm(currentWeapon)
232+
return
236233
end
237234

238235
local playerPed = PlayerData.ped
239236
ClearPedSecondaryTask(playerPed)
240237
if data.throwable then item.throwable = true end
241-
if currentWeapon then Utils.Disarm(currentWeapon) end
238+
if currentWeapon then currentWeapon = Utils.Disarm(currentWeapon) end
242239
local sleep = (PlayerData.job.name == ox.police and (GetWeapontypeGroup(data.hash) == 416676503 or GetWeapontypeGroup(data.hash) == 690389602)) and 400 or 1200
243240
local coords = GetEntityCoords(playerPed, true)
244241
Utils.PlayAnimAdvanced(sleep*2, sleep == 400 and 'reaction@intimidation@cop@unarmed' or 'reaction@intimidation@1h', 'intro', coords.x, coords.y, coords.z, 0, 0, GetEntityHeading(playerPed), 8.0, 3.0, -1, 50, 0.1)
@@ -277,6 +274,7 @@ local function useSlot(slot)
277274
SetPedInfiniteAmmo(playerPed, true, data.hash)
278275
end
279276

277+
currentWeapon = item
280278
TriggerEvent('ox_inventory:currentWeapon', item)
281279
Utils.ItemNotify({item.label, item.name, ox.locale('equipped')})
282280
Wait(sleep)
@@ -392,7 +390,7 @@ function OnPlayerData(key, val)
392390
end
393391
table.wipe(nearbyMarkers)
394392
elseif key == 'dead' and val then
395-
Utils.Disarm(currentWeapon)
393+
currentWeapon = Utils.Disarm(currentWeapon)
396394
TriggerEvent('ox_inventory:closeInventory')
397395
Wait(50)
398396
end
@@ -587,7 +585,7 @@ end)
587585

588586
RegisterNetEvent('ox_inventory:inventoryReturned', function(data)
589587
Utils.Notify({text = ox.locale('items_returned'), duration = 2500})
590-
if currentWeapon then Utils.Disarm(currentWeapon) end
588+
if currentWeapon then currentWeapon = Utils.Disarm(currentWeapon) end
591589
TriggerEvent('ox_inventory:closeInventory')
592590
PlayerData.inventory = data[1]
593591
ox.SetPlayerData('inventory', data[1])
@@ -596,7 +594,7 @@ end)
596594

597595
RegisterNetEvent('ox_inventory:inventoryConfiscated', function(message)
598596
if message then Utils.Notify({text = ox.locale('items_confiscated'), duration = 2500}) end
599-
if currentWeapon then Utils.Disarm(currentWeapon) end
597+
if currentWeapon then currentWeapon = Utils.Disarm(currentWeapon) end
600598
TriggerEvent('ox_inventory:closeInventory')
601599
table.wipe(PlayerData.inventory)
602600
ox.SetPlayerData('weight', 0)
@@ -605,7 +603,7 @@ end)
605603
RegisterNetEvent('ox_inventory:createDrop', function(data, owner, slot)
606604
drops[data[1]] = data[2]
607605
if owner == PlayerData.id and invOpen and #(GetEntityCoords(PlayerData.ped) - data[2]) <= 1 then
608-
if currentWeapon?.slot == slot then Utils.Disarm(currentWeapon) end
606+
if currentWeapon?.slot == slot then currentWeapon = Utils.Disarm(currentWeapon) end
609607

610608
if not IsPedInAnyVehicle(PlayerData.ped, false) then
611609
OpenInventory('drop', data[1])
@@ -758,7 +756,7 @@ RegisterNetEvent('ox_inventory:setPlayerInventory', function(currentDrops, inven
758756
end
759757
end
760758

761-
if currentWeapon and GetSelectedPedWeapon(PlayerData.ped) ~= currentWeapon.hash then Utils.Disarm(currentWeapon) end
759+
if currentWeapon and GetSelectedPedWeapon(PlayerData.ped) ~= currentWeapon.hash then currentWeapon = Utils.Disarm(currentWeapon) end
762760
if ox.parachute and GetPedParachuteState(PlayerData.ped) ~= -1 then
763761
Utils.DeleteObject(ox.parachute)
764762
ox.parachute = false
@@ -806,13 +804,13 @@ RegisterNetEvent('ox_inventory:setPlayerInventory', function(currentDrops, inven
806804
DisableControlAction(0, 140, true)
807805

808806
if not invBusy and currentWeapon.timer ~= 0 and currentWeapon.timer < GetGameTimer() then
807+
currentWeapon.timer = 0
809808
if currentWeapon.metadata.ammo then
810809
TriggerServerEvent('ox_inventory:updateWeapon', 'ammo', currentWeapon.metadata.ammo)
811810
elseif currentWeapon.metadata.durability then
812811
TriggerServerEvent('ox_inventory:updateWeapon', 'melee', currentWeapon.melee)
813812
currentWeapon.melee = 0
814813
end
815-
currentWeapon.timer = 0
816814
elseif currentWeapon.metadata.ammo then
817815
local playerPed = PlayerData.ped
818816
if IsPedShooting(playerPed) then
@@ -832,18 +830,16 @@ RegisterNetEvent('ox_inventory:setPlayerInventory', function(currentDrops, inven
832830
ClearPedTasks(playerPed)
833831
SetCurrentPedWeapon(playerPed, currentWeapon.hash, true)
834832
SetPedCurrentWeaponVisible(playerPed, true, false, false, false)
835-
if currentWeapon?.ammo and ox.autoreload and not Interface.ProgressActive and not IsPedRagdoll(playerPed) and not IsPedFalling(playerPed) and currentWeapon.durability > 0 then
833+
if currentWeapon?.ammo and ox.autoreload and not Interface.ProgressActive and not IsPedRagdoll(playerPed) and not IsPedFalling(playerPed) then
834+
currentWeapon.timer = 0
836835
local ammo = Inventory.Search(1, currentWeapon.ammo)
837836

838837
if ammo[1] then
839838
TriggerServerEvent('ox_inventory:updateWeapon', 'ammo', currentWeapon.metadata.ammo)
840-
currentWeapon.timer = 0
841839
useSlot(ammo[1].slot)
842840
end
843-
844-
end
845-
end
846-
currentWeapon.timer = GetGameTimer() + 400
841+
else currentWeapon.timer = GetGameTimer() + 400 end
842+
else currentWeapon.timer = GetGameTimer() + 400 end
847843
end
848844
elseif IsControlJustReleased(0, 24) then
849845
if currentWeapon.throwable then
@@ -854,6 +850,7 @@ RegisterNetEvent('ox_inventory:setPlayerInventory', function(currentDrops, inven
854850
ClearPedSecondaryTask(playerPed)
855851
RemoveWeaponFromPed(playerPed, currentWeapon.hash)
856852
TriggerServerEvent('ox_inventory:updateWeapon', 'throw')
853+
currentWeapon = nil
857854
TriggerEvent('ox_inventory:currentWeapon')
858855
plyState.invBusy = false
859856
end)
@@ -894,7 +891,7 @@ RegisterNetEvent('esx:onPlayerLogout', function()
894891
PlayerData.loaded = false
895892
ClearInterval(interval)
896893
ClearInterval(tick)
897-
Utils.Disarm(currentWeapon)
894+
currentWeapon = Utils.Disarm(currentWeapon)
898895
end)
899896

900897
RegisterNetEvent('ox_inventory:viewInventory', function(data)
@@ -975,7 +972,7 @@ RegisterNUICallback('giveItem', function(data, cb)
975972
else return end
976973
if passenger then
977974
TriggerServerEvent('ox_inventory:giveItem', data.slot, passenger, data.count)
978-
if data.slot == currentWeapon?.slot then Utils.Disarm(currentWeapon) end
975+
if data.slot == currentWeapon?.slot then currentWeapon = Utils.Disarm(currentWeapon) end
979976
end
980977
end
981978
else
@@ -984,7 +981,7 @@ RegisterNUICallback('giveItem', function(data, cb)
984981
target = GetPlayerServerId(NetworkGetPlayerIndexFromPed(target))
985982
Utils.PlayAnim(2000, 'mp_common', 'givetake1_a', 1.0, 1.0, -1, 50, 0.0, 0, 0, 0)
986983
TriggerServerEvent('ox_inventory:giveItem', data.slot, target, data.count)
987-
if data.slot == currentWeapon?.slot then Utils.Disarm(currentWeapon) end
984+
if data.slot == currentWeapon?.slot then currentWeapon = Utils.Disarm(currentWeapon) end
988985
end
989986
end
990987
end)

modules/inventory/server.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,9 @@ RegisterServerEvent('ox_inventory:updateWeapon', function(action, value, slot)
906906
end
907907

908908
if action ~= 'throw' then TriggerClientEvent('ox_inventory:updateInventory', source, {{item = weapon}}, {left=inventory.weight}) end
909+
909910
if weapon.metadata.durability and weapon.metadata.durability < 1 and action ~= 'load' and action ~= 'component' then
910-
TriggerClientEvent('ox_inventory:disarm', source, false)
911+
TriggerClientEvent('ox_inventory:disarm', source)
911912
end
912913
end
913914
end

modules/player/client.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if ox.esx then
5050
PlayerData.cuffed = not PlayerData.cuffed
5151
LocalPlayer.state:set('busy', PlayerData.cuffed, false)
5252
if PlayerData.cuffed then
53-
Utils.Disarm(currentWeapon)
53+
currentWeapon = Utils.Disarm(currentWeapon)
5454
if invOpen then TriggerEvent('ox_inventory:closeInventory') end
5555
end
5656
end)

modules/utils/client.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ function Utils.Disarm(currentWeapon, newSlot)
8282
TriggerServerEvent('ox_inventory:updateWeapon', ammo and 'ammo' or 'melee', ammo or currentWeapon.melee, newSlot)
8383
end
8484

85+
currentWeapon = nil
8586
TriggerEvent('ox_inventory:currentWeapon')
8687
end
8788
end
8889

8990
function Utils.ClearWeapons(currentWeapon)
90-
Utils.Disarm(currentWeapon)
91+
currentWeapon = Utils.Disarm(currentWeapon)
9192
RemoveAllPedWeapons(PlayerData.ped, true)
9293
if ox.parachute then
9394
local chute = `GADGET_PARACHUTE`

0 commit comments

Comments
 (0)