[HELP] What is wrong with my /guns script

I want a script that is going to give me the gun that i type and the amount of ammo, like below, but my script doesn’t seems to works
EX: /armas “Name of the gun” “Ammo”

Thats not how that works. You can’t just pass a Guns table to the GetHashKey method and expect it to give you both weapons. That’s what loops are for. BUT if you wanna give certain weapon you are gonna need to alter your code a bit.

1 Like

I Don’t want both guns, i just want the gun that i type.
If i want a pistol i can /guns pistol 10
If i want a smg i can /guns smg 10

RegisterCommand("armas", function(source, args, raw)
	local weapons = {pistol = "weapon_pistol", smg = "weapon_smg"}
	local ped = GetPlayerPed(-1)
	local selectedWeapon = weapons["pistol"] -- Default Weapon
	local selectedAmmo = 60 -- Default Ammo
	local chosenWeapon = tostring(string.lower(args[1]))
	local chosenAmmo = tonumber(args[2])

	if chosenWeapon ~= nil then if weapons[chosenWeapon] ~= nil then selectedWeapon = weapons[chosenWeapon] end end
	if chosenAmmo ~= nil then selectedAmmo = chosenAmmo end
	GiveWeaponToPed(ped, GetHashKey(weapons[selectedWeapon]), selectedAmmo, false, true)
end, false)

Try that.

I would recommend you put the weapons table somewhere else as putting that in a command will take up some space the more weapons you add but that is up for you to decide.

EDIT. I Just updated. Try the new one. The first one will error due to me forgetting to target the weapons table.

The command is going to be like /armas “Name of the gun” “Ammo” ?
Like /armas pistol 10

Yes. it should be.

It’s not working for me

Well you need to tell me if there are any errors… I am freehand writing it. I don’t have a server open. Telling me its not working isn’t helping me fix it.

Does this help you ?

Scroll up one line.

try this

RegisterCommand("armas", function(source, args, raw)
	local weapons = {pistol = "weapon_pistol", smg = "weapon_smg"}
	local ped = GetPlayerPed(-1)
	local selectedWeapon = weapons["pistol"] -- Default Weapon
	local selectedAmmo = 60 -- Default Ammo

	-- Player Selected Weapon
	local chosenWeapon = nil
	if args[1] ~= nil then chosenWeapon = tostring(string.lower(args[1])) end

	-- Player Selected Ammo
	local chosenAmmo = nil
	if chosenAmmo ~= nil then chosenAmmo = tonumber(args[2]) end

	if chosenWeapon ~= nil then if weapons[chosenWeapon] ~= nil then selectedWeapon = weapons[chosenWeapon] end end
	if chosenAmmo ~= nil then selectedAmmo = chosenAmmo end
	GiveWeaponToPed(ped, GetHashKey(weapons[selectedWeapon]), selectedAmmo, false, true)
end, false)

It doesn’t give me an error in console, but it also doesn’t give me the gun
i’m using /armas pistol 10

Here you go

RegisterCommand("armas", function(source, args, raw)
	local weapons = {pistol = "WEAPON_PISTOL", smg = "WEAPON_SMG"}
	local ped = GetPlayerPed(-1)
	local selectedWeapon = weapons["pistol"] -- Default Weapon
	local selectedAmmo = 60 -- Default Ammo

	-- Player Selected Weapon
	local chosenWeapon = nil
	if args[1] ~= nil then chosenWeapon = tostring(string.lower(args[1])) end

	-- Player Selected Ammo
	local chosenAmmo = nil
	if args[2] ~= nil ~= nil then chosenAmmo = tonumber(args[2]) end

	if chosenWeapon ~= nil then if weapons[chosenWeapon] ~= nil then selectedWeapon = weapons[chosenWeapon] end end
	if chosenAmmo ~= nil then selectedAmmo = chosenAmmo end

	GiveWeaponToPed(ped, GetHashKey(selectedWeapon), selectedAmmo, true, true)
end, false)

PogChamp, it worked, thanks dude, sorry for making you waste time with this

1 Like