From 74f01944f1ff82e18c964c576d59ab7d8be4cee0 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Wed, 2 Jun 2021 12:41:46 +1000 Subject: [PATCH] improvement+fix(client/main): Save weapon ammo properly; create a timer for saving ammo instead of triggering an event every frame --- client/main.lua | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/client/main.lua b/client/main.lua index 1182cd2b..0cdcc21e 100644 --- a/client/main.lua +++ b/client/main.lua @@ -371,10 +371,17 @@ end function StartServerSyncLoops() -- keep track of ammo Citizen.CreateThread(function() + local currentWeapon = {timer=0} while ESX.PlayerLoaded do - Citizen.Wait(1000) + local sleep = 5 - local letSleep = true + if currentWeapon.timer == sleep then + local ammoCount = GetAmmoInPedWeapon(ESX.PlayerData.ped, currentWeapon.hash) + TriggerServerEvent('esx:updateWeaponAmmo', currentWeapon.name, ammoCount) + currentWeapon.timer = 0 + elseif currentWeapon.timer > sleep then + currentWeapon.timer = currentWeapon.timer - sleep + end if IsPedArmed(ESX.PlayerData.ped, 4) then if IsPedShooting(ESX.PlayerData.ped) then @@ -382,14 +389,15 @@ function StartServerSyncLoops() local weapon = ESX.GetWeaponFromHash(weaponHash) if weapon then - local ammoCount = GetAmmoInPedWeapon(ESX.PlayerData.ped, weaponHash) - TriggerServerEvent('esx:updateWeaponAmmo', weapon.name, ammoCount) + currentWeapon.name = weapon.name + currentWeapon.hash = weaponHash + currentWeapon.timer = 100 * sleep end end + else + sleep = 200 end - if letSleep then - Citizen.Wait(500) - end + Citizen.Wait(sleep) end end)