Error Getting weapon on esx_policejob

Hi. When I try to get a weapon on the armory, It doesn’t open anything and It gives me this error:

Someone know how to fix this?’ It’s the last bug I have to fix before I open my server.
It’s the first RP Portuguese server with 1024 slots :smiley:

If you actually provide the coding where the line exists, it would help the community better to solve your problem.

1 Like

Here it is :smiley:

Paste the whole function for function OpenBuyWeaponsMenu

This is the code:

function OpenBuyWeaponsMenu()
local elements = {}
local playerPed = PlayerPedId()
PlayerData = ESX.GetPlayerData()

for k,v in ipairs(Config.AuthorizedWeapons[PlayerData.job.grade_name]) do
	local weaponNum, weapon = ESX.GetWeapon(v.weapon)
	local components, label = {}
	local hasWeapon = HasPedGotWeapon(playerPed, GetHashKey(v.weapon), false)

	if v.components then
		for i=1, #v.components do
			if v.components[i] then
				local component = weapon.components[i]
				local hasComponent = HasPedGotWeaponComponent(playerPed, GetHashKey(v.weapon), component.hash)

				if hasComponent then
					label = ('%s: <span style="color:green;">%s</span>'):format(component.label, _U('armory_owned'))
				else
					if v.components[i] > 0 then
						label = ('%s: <span style="color:green;">%s</span>'):format(component.label, _U('armory_item', ESX.Math.GroupDigits(v.components[i])))
					else
						label = ('%s: <span style="color:green;">%s</span>'):format(component.label, _U('armory_free'))
					end
				end

				table.insert(components, {
					label = label,
					componentLabel = component.label,
					hash = component.hash,
					name = component.name,
					price = v.components[i],
					hasComponent = hasComponent,
					componentNum = i
				})
			end
		end
	end

By the way, thanks for helping me

I’m not sure why you have “PlayerData = ESX.GetPlayerData()” up near your locals, and then changed it in the “Config.AuthorizedWeapons[PlayerData.job.grade_name]” area… mine works fine the way I have it set up and I don’t have that in mine…

function OpenBuyWeaponsMenu()
	local elements = {}
	local playerPed = PlayerPedId()

	for k,v in ipairs(Config.AuthorizedWeapons[ESX.PlayerData.job.grade_name]) do
		local weaponNum, weapon = ESX.GetWeapon(v.weapon)
		local components, label = {}
		local hasWeapon = HasPedGotWeapon(playerPed, GetHashKey(v.weapon), false)

		if v.components then
			for i=1, #v.components do
				if v.components[i] then
					local component = weapon.components[i]
					local hasComponent = HasPedGotWeaponComponent(playerPed, GetHashKey(v.weapon), component.hash)

					if hasComponent then
						label = ('%s: <span style="color:green;">%s</span>'):format(component.label, _U('armory_owned'))
					else
						if v.components[i] > 0 then
							label = ('%s: <span style="color:green;">%s</span>'):format(component.label, _U('armory_item', ESX.Math.GroupDigits(v.components[i])))
						else
							label = ('%s: <span style="color:green;">%s</span>'):format(component.label, _U('armory_free'))
						end
					end

					table.insert(components, {
						label = label,
						componentLabel = component.label,
						hash = component.hash,
						name = component.name,
						price = v.components[i],
						hasComponent = hasComponent,
						componentNum = i
					})
				end
			end
		end

		if hasWeapon and v.components then
			label = ('%s: <span style="color:green;">></span>'):format(weapon.label)
		elseif hasWeapon and not v.components then
			label = ('%s: <span style="color:green;">%s</span>'):format(weapon.label, _U('armory_owned'))
		else
			if v.price > 0 then
				label = ('%s: <span style="color:green;">%s</span>'):format(weapon.label, _U('armory_item', ESX.Math.GroupDigits(v.price)))
			else
				label = ('%s: <span style="color:green;">%s</span>'):format(weapon.label, _U('armory_free'))
			end
		end

		table.insert(elements, {
			label = label,
			weaponLabel = weapon.label,
			name = weapon.name,
			components = components,
			price = v.price,
			hasWeapon = hasWeapon
		})
	end

	ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'armory_buy_weapons', {
		title    = _U('armory_weapontitle'),
		align    = 'top-right',
		elements = elements
	}, function(data, menu)
		if data.current.hasWeapon then
			if #data.current.components > 0 then
				OpenWeaponComponentShop(data.current.components, data.current.name, menu)
			end
		else
			ESX.TriggerServerCallback('esx_policejob:buyWeapon', function(bought)
				if bought then
					if data.current.price > 0 then
						exports['mythic_notify']:DoHudText('success', _U('armory_bought', data.current.weaponLabel, ESX.Math.GroupDigits(data.current.price)))
					end

					menu.close()
					OpenBuyWeaponsMenu()
				else
					exports['mythic_notify']:DoHudText('error', _U('armory_money'))
				end
			end, data.current.name, 1)
		end
	end, function(data, menu)
		menu.close()
	end)
end

You may have to change notifications if you copy and paste from mine as I used mythic_notify

Can you send me this function too?

function OpenBuyWeaponsMenu()
local elements = {}
local playerPed = PlayerPedId()

for k,v in ipairs(Config.AuthorizedWeapons[ESX.PlayerData.job.grade_name]) do
	local weaponNum, weapon = ESX.GetWeapon(v.weapon)
	local components, label = {}
	local hasWeapon = HasPedGotWeapon(playerPed, GetHashKey(v.weapon), false)

	if v.components then
		for i=1, #v.components do
			if v.components[i] then
				local component = weapon.components[i]
				local hasComponent = HasPedGotWeaponComponent(playerPed, GetHashKey(v.weapon), component.hash)

				if hasComponent then
					label = ('%s: <span style="color:green;">%s</span>'):format(component.label, _U('armory_owned'))
				else
					if v.components[i] > 0 then
						label = ('%s: <span style="color:green;">%s</span>'):format(component.label, _U('armory_item', ESX.Math.GroupDigits(v.components[i])))
					else
						label = ('%s: <span style="color:green;">%s</span>'):format(component.label, _U('armory_free'))
					end
				end

				table.insert(components, {
					label = label,
					componentLabel = component.label,
					hash = component.hash,
					name = component.name,
					price = v.components[i],
					hasComponent = hasComponent,
					componentNum = i
				})
			end
		end
	end

	if hasWeapon and v.components then
		label = ('%s: <span style="color:green;">></span>'):format(weapon.label)
	elseif hasWeapon and not v.components then
		label = ('%s: <span style="color:green;">%s</span>'):format(weapon.label, _U('armory_owned'))
	else
		if v.price > 0 then
			label = ('%s: <span style="color:green;">%s</span>'):format(weapon.label, _U('armory_item', ESX.Math.GroupDigits(v.price)))
		else
			label = ('%s: <span style="color:green;">%s</span>'):format(weapon.label, _U('armory_free'))
		end
	end

	table.insert(elements, {
		label = label,
		weaponLabel = weapon.label,
		name = weapon.name,
		components = components,
		price = v.price,
		hasWeapon = hasWeapon
	})
end

ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'armory_buy_weapons', {
	title    = _U('armory_weapontitle'),
	align    = 'top-right',
	elements = elements
}, function(data, menu)
	if data.current.hasWeapon then
		if #data.current.components > 0 then
			OpenWeaponComponentShop(data.current.components, data.current.name, menu)
		end
	else
		ESX.TriggerServerCallback('esx_policejob:buyWeapon', function(bought)
			if bought then
				if data.current.price > 0 then
					exports['mythic_notify']:DoHudText('success', _U('armory_bought', data.current.weaponLabel, ESX.Math.GroupDigits(data.current.price)))
				end

				menu.close()
				OpenBuyWeaponsMenu()
			else
				exports['mythic_notify']:DoHudText('error', _U('armory_money'))
			end
		end, data.current.name, 1)
	end
end, function(data, menu)
	menu.close()
end)

end

That was the whole OpenBuyWeaponsMenu() function I sent you before.

ahh ok

Lol its giving me this now :smiley:

Make sure that if you added job_grades to your database, that you also have them included in your config file for the weapons available. And make sure you have police job and the table in your database for the job_grades.

I have a modified version of the policejob that i edited myself. So mine looks way different than yours.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.