Weaponsync [ESX]

Hi everyone,

I hope you all have an awesome day. I have a small issue. I’m using a script called Weaponsync, and when a player joins it doesn’t update the weapon wheel. I’m sure there is a trigger somewhere because it does its thing when I’m getting revived, but I’m just unable to find it.

client.lua:

ESX = nil

local Weapons = {}
local AmmoTypes = {}

local PlayerData = nil
local AmmoInClip = {}

local CurrentWeapon = nil

local IsShooting = false
local AmmoBefore = 0

for name,item in pairs(Config.Weapons) do
  Weapons[GetHashKey(name)] = item
end

for name,item in pairs(Config.AmmoTypes) do
  AmmoTypes[GetHashKey(name)] = item
end

Citizen.CreateThread(function()
  while ESX == nil do
    TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
    Citizen.Wait(0)
  end
end)

function GetAmmoItemFromHash(hash)
  for name,item in pairs(Config.Weapons) do
    if hash == GetHashKey(item.name) then
      if item.ammo then
        return item.ammo
      else
        return nil
      end
    end
  end
  return nil
end

function GetInventoryItem(name)
  local inventory = PlayerData.inventory
  for i=1, #inventory, 1 do
    if inventory[i].name == name then
      return inventory[i]
    end
  end
  return nil
end

function RebuildLoadout()
  
  while not PlayerData do
    Citizen.Wait(0)
  end
  
  local playerPed = GetPlayerPed(-1)

  for weaponHash,v in pairs(Weapons) do
    local item = GetInventoryItem(v.item)
    if item and item.count > 0 then
      local ammo = 0
      local ammoType = GetPedAmmoTypeFromWeapon(playerPed, weaponHash)

      if ammoType and AmmoTypes[ammoType] then
        local ammoItem = GetInventoryItem(AmmoTypes[ammoType].item)
        if ammoItem then
          ammo = ammoItem.count
        end
      end

      if item.name == "fireextinguisher" then
        ammo = 1000
      end
      
      if HasPedGotWeapon(playerPed, weaponHash, false) then
        if GetAmmoInPedWeapon(playerPed, weaponHash) ~= ammo then
          SetPedAmmo(playerPed, weaponHash, ammo)
        end
      else
        -- Weapon is missing, give it to the player
        GiveWeaponToPed(playerPed, weaponHash, ammo, false, false)
      end
    elseif HasPedGotWeapon(playerPed, weaponHash, false) then
      -- Weapon doesn't belong in loadout
      RemoveWeaponFromPed(playerPed, weaponHash)
    end
  end

end

function RemoveUsedAmmo()  
  local playerPed = GetPlayerPed(-1)
  local AmmoAfter = GetAmmoInPedWeapon(playerPed, CurrentWeapon)
  local ammoType = AmmoTypes[GetPedAmmoTypeFromWeapon(playerPed, CurrentWeapon)]
  
  if ammoType and ammoType.item then
    local ammoDiff = AmmoBefore - AmmoAfter
    if ammoDiff > 0 then
      TriggerServerEvent('esx:discardInventoryItem', ammoType.item, ammoDiff)
    end
  end

  return AmmoAfter
end

RegisterNetEvent('esx:playerLoaded')
AddEventHandler('esx:playerLoaded', function(xPlayer)
  PlayerData = xPlayer
  RebuildLoadout()
end)

RegisterNetEvent('esx:modelChanged')
AddEventHandler('esx:modelChanged', function()
  RebuildLoadout()
end)

AddEventHandler('playerSpawned', function()
  RebuildLoadout()
end)

AddEventHandler('skinchanger:modelLoaded', function()
  RebuildLoadout()
end)

RegisterNetEvent('esx:addInventoryItem')
AddEventHandler('esx:addInventoryItem', function(name, count)
  Citizen.Wait(1) -- Wait a tick to make sure ESX has updated PlayerData
  PlayerData = ESX.GetPlayerData()
  RebuildLoadout()
  if CurrentWeapon then
    AmmoBefore = GetAmmoInPedWeapon(GetPlayerPed(-1), CurrentWeapon)
  end
end)

RegisterNetEvent('esx:removeInventoryItem')
AddEventHandler('esx:removeInventoryItem', function(name, count)
  Citizen.Wait(1) -- Wait a tick to make sure ESX has updated PlayerData
  PlayerData = ESX.GetPlayerData()
  RebuildLoadout()
  if CurrentWeapon then
    AmmoBefore = GetAmmoInPedWeapon(GetPlayerPed(-1), CurrentWeapon)
  end
end)

Citizen.CreateThread(function()
  while true do
    Citizen.Wait(0)
    
    local playerPed = GetPlayerPed(-1)

    if CurrentWeapon ~= GetSelectedPedWeapon(playerPed) then
      IsShooting = false
      RemoveUsedAmmo()
      CurrentWeapon = GetSelectedPedWeapon(playerPed)
      AmmoBefore = GetAmmoInPedWeapon(playerPed, CurrentWeapon)
    end

    if IsPedShooting(playerPed) and not IsShooting then
      IsShooting = true
    elseif IsShooting and IsControlJustReleased(0, 24) then
      IsShooting = false
      AmmoBefore = RemoveUsedAmmo()
    elseif not IsShooting and IsControlJustPressed(0, 45) then
      AmmoBefore = GetAmmoInPedWeapon(playerPed, CurrentWeapon)
    end
  end
end)

server.lua:

ESX = nil

TriggerEvent('esx:getSharedObject', function(obj)
	ESX = obj
end)

RegisterServerEvent('esx:discardInventoryItem')
AddEventHandler('esx:discardInventoryItem', function(item, count)

	local _source = source
	local xPlayer = ESX.GetPlayerFromId(source)

  xPlayer.removeInventoryItem(item, count, true)

end)

RegisterServerEvent('esx:modelChanged')
AddEventHandler('esx:modelChanged', function(id)
	TriggerClientEvent('esx:modelChanged', id)
end)

ESX.RegisterUsableItem('pistol_ammo_box', function(source)
	local xPlayer  = ESX.GetPlayerFromId(source)
	xPlayer.removeInventoryItem('pistol_ammo_box', 1)
	xPlayer.addInventoryItem('pistol_ammo', 24)
end)

ESX.RegisterUsableItem('smg_ammo_box', function(source)
	local xPlayer  = ESX.GetPlayerFromId(source)
	xPlayer.removeInventoryItem('smg_ammo_box', 1)
	xPlayer.addInventoryItem('smg_ammo', 30)
end)

ESX.RegisterUsableItem('rifle_ammo_box', function(source)
	local xPlayer  = ESX.GetPlayerFromId(source)
	xPlayer.removeInventoryItem('rifle_ammo_box', 1)
	xPlayer.addInventoryItem('rifle_ammo', 30)
end)

ESX.RegisterUsableItem('shotgun_ammo_box', function(source)
	local xPlayer  = ESX.GetPlayerFromId(source)
	xPlayer.removeInventoryItem('shotgun_ammo_box', 1)
	xPlayer.addInventoryItem('shotgun_ammo', 16)
end)

config.lua:

Config = {}
Config.Locale = "en"

Config.Weapons = {
  WEAPON_KNIFE = { item = 'knife', label = _U('weapon_knife') },
  WEAPON_NIGHTSTICK = { item = 'nightstick', label = _U('weapon_nightstick') },
  WEAPON_HAMMER = { item = 'hammer', label = _U('weapon_hammer') },
  WEAPON_BAT = { item = 'bat', label = _U('weapon_bat') },
  WEAPON_GOLFCLUB = { item = 'golfclub', label = _U('weapon_golfclub') },
  WEAPON_CROWBAR = { item = 'crowbar', label = _U('weapon_crowbar') },
  WEAPON_PISTOL = { item = 'pistol', label = _U('weapon_pistol') },
  WEAPON_COMBATPISTOL = { item = 'combatpistol', label = _U('weapon_combatpistol') },
  WEAPON_APPISTOL = { item = 'appistol', label = _U('weapon_appistol') },
  WEAPON_PISTOL50 = { item = 'pistol50', label = _U('weapon_pistol50') },
  WEAPON_MICROSMG = { item = 'microsmg', label = _U('weapon_microsmg') },
  WEAPON_SMG = { item = 'smg', label = _U('weapon_smg') },
  WEAPON_ASSAULTSMG = { item = 'assaultsmg', label = _U('weapon_assaultsmg') },
  WEAPON_ASSAULTRIFLE = { item = 'assaultrifle', label = _U('weapon_assaultrifle') },
  WEAPON_CARBINERIFLE = { item = 'carbinerifle', label = _U('weapon_carbinerifle') },
  WEAPON_ADVANCEDRIFLE = { item = 'advancedrifle', label = _U('weapon_advancedrifle') },
  WEAPON_MG = { item = 'mg', label = _U('weapon_mg') },
  WEAPON_COMBATMG = { item = 'combatmg', label = _U('weapon_combatmg') },
  WEAPON_PUMPSHOTGUN = { item = 'pumpshotgun', label = _U('weapon_pumpshotgun') },
  WEAPON_SAWNOFFSHOTGUN = { item = 'sawnoffshotgun', label = _U('weapon_sawnoffshotgun') },
  WEAPON_ASSAULTSHOTGUN = { item = 'assaultshotgun', label = _U('weapon_assaultshotgun') },
  WEAPON_BULLPUPSHOTGUN = { item = 'bullpupshotgun', label = _U('weapon_bullpupshotgun') },
  WEAPON_STUNGUN = { item = 'stungun', label = _U('weapon_stungun') },
  WEAPON_SNIPERRIFLE = { item = 'sniperrifle', label = _U('weapon_sniperrifle') },
  WEAPON_HEAVYSNIPER = { item = 'heavysniper', label = _U('weapon_heavysniper') },
  WEAPON_REMOTESNIPER = { item = 'remotesniper', label = _U('weapon_remotesniper') },
  WEAPON_GRENADELAUNCHER = { item = 'grenadelauncher', label = _U('weapon_grenadelauncher') },
  WEAPON_RPG = { item = 'rpg', label = _U('weapon_rpg') },
  WEAPON_STINGER = { item = 'stinger', label = _U('weapon_stinger') },
  WEAPON_MINIGUN = { item = 'minigun', label = _U('weapon_minigun') },
  WEAPON_GRENADE = { item = 'grenade', label = _U('weapon_grenade') },
  WEAPON_STICKYBOMB = { item = 'stickybomb', label = _U('weapon_stickybomb') },
  WEAPON_SMOKEGRENADE = { item = 'smokegrenade', label = _U('weapon_smokegrenade') },
  WEAPON_BZGAS = { item = 'bzgas', label = _U('weapon_bzgas') },
  WEAPON_MOLOTOV = { item = 'molotov', label = _U('weapon_molotov') },
  WEAPON_FIREEXTINGUISHER = { item = 'fireextinguisher', label = _U('weapon_fireextinguisher') },
  WEAPON_PETROLCAN = { item = 'petrolcan', label = _U('weapon_petrolcan') },
  WEAPON_DIGISCANNER = { item = 'digiscanner', label = _U('weapon_digiscanner') },
  WEAPON_BALL = { item = 'ball', label = _U('weapon_ball') },
  WEAPON_SNSPISTOL = { item = 'snspistol', label = _U('weapon_snspistol') },
  WEAPON_BOTTLE = { item = 'bottle', label = _U('weapon_bottle') },
  WEAPON_GUSENBERG = { item = 'gusenberg', label = _U('weapon_gusenberg') },
  WEAPON_SPECIALCARBINE = { item = 'specialcarbine', label = _U('weapon_specialcarbine') },
  WEAPON_HEAVYPISTOL = { item = 'heavypistol', label = _U('weapon_heavypistol') },
  WEAPON_BULLPUPRIFLE = { item = 'bullpuprifle', label = _U('weapon_bullpuprifle') },
  WEAPON_DAGGER = { item = 'dagger', label = _U('weapon_dagger') },
  WEAPON_VINTAGEPISTOL = { item = 'vintagepistol', label = _U('weapon_vintagepistol') },
  WEAPON_FIREWORK = { item = 'firework', label = _U('weapon_firework') },
  --WEAPON_MUSKET = { item = 'musket', label = _U('weapon_musket') },
  WEAPON_HEAVYSHOTGUN = { item = 'heavyshotgun', label = _U('weapon_heavyshotgun') },
  WEAPON_MARKSMANRIFLE = { item = 'marksmanrifle', label = _U('weapon_marksmanrifle') },
  WEAPON_HOMINGLAUNCHER = { item = 'hominglauncher', label = _U('weapon_hominglauncher') },
  WEAPON_PROXMINE = { item = 'proxmine', label = _U('weapon_proxmine') },
  WEAPON_SNOWBALL = { item = 'snowball', label = _U('weapon_snowball') },
  WEAPON_FLAREGUN = { item = 'flaregun', label = _U('weapon_flaregun') },
  WEAPON_GARBAGEBAG = { item = 'garbagebag', label = _U('weapon_garbagebag') },
  WEAPON_HANDCUFFS = { item = 'handcuffs', label = _U('weapon_handcuffs') },
  WEAPON_COMBATPDW = { item = 'combatpdw', label = _U('weapon_combatpdw') },
  WEAPON_MARKSMANPISTOL = { item = 'marksmanpistol', label = _U('weapon_marksmanpistol') },
  WEAPON_KNUCKLE = { item = 'knuckle', label = _U('weapon_knuckle') },
  WEAPON_HATCHET = { item = 'hatchet', label = _U('weapon_hatchet') },
  WEAPON_RAILGUN = { item = 'railgun', label = _U('weapon_railgun') },
  WEAPON_MACHETE = { item = 'machete', label = _U('weapon_machete') },
  WEAPON_MACHINEPISTOL = { item = 'machinepistol', label = _U('weapon_machinepistol') },
  WEAPON_SWITCHBLADE = { item = 'switchblade', label = _U('weapon_switchblade') },
  WEAPON_REVOLVER = { item = 'revolver', label = _U('weapon_revolver') },
  WEAPON_DBSHOTGUN = { item = 'dbshotgun', label = _U('weapon_dbshotgun') },
  WEAPON_COMPACTRIFLE = { item = 'compactrifle', label = _U('weapon_compactrifle') },
  WEAPON_AUTOSHOTGUN = { item = 'autoshotgun', label = _U('weapon_autoshotgun') },
  WEAPON_BATTLEAXE = { item = 'battleaxe', label = _U('weapon_battleaxe') },
  WEAPON_COMPACTLAUNCHER = { item = 'compactlauncher', label = _U('weapon_compactlauncher') },
  WEAPON_MINISMG = { item = 'minismg', label = _U('weapon_minismg') },
  WEAPON_PIPEBOMB = { item = 'pipebomb', label = _U('weapon_pipebomb') },
  WEAPON_POOLCUE = { item = 'poolcue', label = _U('weapon_poolcue') },
  WEAPON_WRENCH = { item = 'wrench', label = _U('weapon_wrench') },
  WEAPON_FLASHLIGHT = { item = 'flashlight', label = _U('weapon_flashlight') },
  GADGET_NIGHTVISION = { item = 'nightvision', label = _U('gadget_nightvision') },
  GADGET_PARACHUTE = { item = 'parachute', label = _U('gadget_parachute') },
  WEAPON_FLARE = { item = 'flare', label = _U('weapon_flare') }, 
  WEAPON_SNSPISTOL_MK2 = { item = 'snspistol_mk2', label = _U('weapon_snspistol_mk2') }, 
  WEAPON_REVOLVER_MK2 = { item = 'revolver_mk2', label = _U('weapon_revolver_mk2') },  
  WEAPON_DOUBLEACTION = { item = 'doubleaction', label = _U('weapon_doubleaction') },    
  WEAPON_SPECIALCARBINE_MK2 = { item = 'specialcarbine_mk2', label = _U('weapon_specialcarabine_mk2') },  
  WEAPON_BULLPUPRIFLE_MK2 = { item = 'bullpuprifle_mk2', label = _U('weapon_bullpruprifle_mk2') },   
  WEAPON_PUMPSHOTGUN_MK2 = { item = 'pumpshotgun_mk2', label = _U('weapon_pumpshotgun_mk2') },
  WEAPON_MARKSMANRIFLE_MK2 = { item = 'marksmanrifle_mk2', label = _U('weapon_marksmanrifle_mk2') },  
  WEAPON_ASSAULTRIFLE_MK2 = { item = 'assaultrifle_mk2', label = _U('weapon_assaultrifle_mk2') },
  WEAPON_CARBINERIFLE_MK2 = { item = 'carbinerifle_mk2', label = _U('weapon_carbinerifle_mk2') },  
  WEAPON_COMBATMG_MK2 = { item = 'combatmg_mk2', label = _U('weapon_combatmg_mk2') },   
  WEAPON_HEAVYSNIPER_MK2 = { item = 'heavysniper_mk2', label = _U('weapon_heavysniper_mk2') },   
  WEAPON_PISTOL_MK2 = { item = 'pistol_mk2', label = _U('weapon_pistol_mk2') },   
  WEAPON_SMG_MK2 = { item = 'smg_mk2', label = _U('weapon_smg_mk2') }
}

Config.AmmoTypes = {
  AMMO_PISTOL = { item = 'pistol_ammo' },
  AMMO_SMG = { item = 'smg_ammo' },
  AMMO_FIREWORK = { item = 'firework_ammo' },
  AMMO_RIFLE = { item = 'rifle_ammo' },
  AMMO_MG = { item = 'mg_ammo' },
  AMMO_SHOTGUN = { item = 'shotgun_ammo' },
  AMMO_STUNGUN = { item = 'stungun_ammo' },
  AMMO_SNIPER = { item = 'sniper_ammo' },
  AMMO_SNIPER_REMOTE = { item = 'sniper_remote_ammo' },
  AMMO_MINIGUN = { item = 'minigun_ammo' },
  AMMO_GRENADELAUNCHER = { item = 'grenadelauncher_ammo' },
  AMMO_GRENADELAUNCHER_SMOKE = { item = 'grenadelauncher_smoke_ammo' },
  AMMO_RPG = { item = 'rpg_ammo' },
  AMMO_STINGER = { item = 'stinger_ammo' },
  AMMO_BALL = { item = 'ball' },
  AMMO_STICKYBOMB = { item = 'stickybomb' },
  AMMO_SMOKEGRENADE = { item = 'smokegrenade' },
  AMMO_BZGAS = { item = 'gzgas_ammo' },
  AMMO_FLARE = { item = 'flare_ammo' },
  AMMO_MOLOTOV = { item = 'molotov' },
  AMMO_TANK = { item = 'tank_ammo' },
  AMMO_SPACE_ROCKET = { item = 'space_rocket_ammo' },
  AMMO_PLANE_ROCKET = { item = 'plane_rocket_ammo' },
  AMMO_PLAYER_LASER = { item = 'player_laser_ammo' },
  AMMO_ENEMY_LASER = { item = 'enemy_laser_ammo' },
  AMMO_BIRD_CRAP = { item = 'bird_crap_ammo' }
}

__resource.lua:

resource_manifest_version '44febabe-d386-4d18-afbe-5e627f4af937'

client_scripts {
  '@es_extended/locale.lua',
  "config.lua",
  "client.lua"
}

server_scripts {
  '@es_extended/locale.lua',
  "config.lua",
  "server.lua"
}

I hope anyone is able to find and fix the issue. Ps: I’m very blind sometimes so please don’t hate me, I’ve literally searched everywhere.

Yes, I’ve sent every single file, don’t hate me xD I’m just trying to make it clear and easy to read for anyone that wants to help me.

Many thanks!

3 Likes

Also when I remove a weapon out of my inventory it doesn’t rebuild the weapon wheel.

1 Like

Hey did you manage to figure this out as im having the same isse

1 Like

¿How?

Hey guys I fixed the Problem with Weapons not loading at the spawn by adding a wait time. i think it didnt load properly because it tryes to Rebuild the Loadout before getting the data but I am not sure.
This is what I changed in the client.lua

Before:
RegisterNetEvent(‘esx:playerLoaded’)
AddEventHandler(‘esx:playerLoaded’, function(xPlayer)
PlayerData = xPlayer
RebuildLoadout()
end)

After:
RegisterNetEvent(‘esx:playerLoaded’)
AddEventHandler(‘esx:playerLoaded’, function(xPlayer)
PlayerData = xPlayer
Citizen.Wait(5000)
RebuildLoadout()
end)

2 Likes

hello nice script but when i drob the gun

ammo is dublicate do you know how can i fix it ?

sorry for english

2 Likes

mine adds just 1 bullet

Very Nice Script but when a Player drops his Weapon he still can shoot with it. After a Restart the Weapon is gone

1 Like

Hi, when I spawn a weapon in via giveitem I also get the weapon as a normal weapon in my inventory so i get the weapon 2 times in 2 different things.

1 time in the weapon sync version and 1 time in this format: SMG - 0 Round(s)

1 Like

ESX version conflict issue

Dont display weapons in inv only items then

1 Like

How do we disable showing the weapons in inventory? And just show the items?

Is that fixable?

Config normally

Should be, i fixed it too

Hey, been using this and had an issue where weapons classes as items can’t be used within inventory hotbars, this is with all inv I have tried, weapons can’t be pulled from hot bar if it’s an item, any help?

Where do I turn that off?