GiveWeaponToPed help

Hi everyone,

I’m working on a script, it’s a ped who sell some weapons, but i’m a beginner in LUA so i need some help…

The ped is nice and when i talk to him, it open the menu BUT, when i choose my weapon, nothing happen… It could be from the Server.lua side, cause i dont really know how to use it.

Client:

function ThrowMenu()  -- here's my menu
    ClearMenu()
    options.menu_title = "Arme de lancé"
	Menu.addButton("Fumigène","AchatArme(hash, prix)",nil)
	Menu.addButton("Coctail molotov","AchatArme('WEAPON_Molotov', 1000)",nil) -- I try to buy this weapon
	Menu.addButton("Bidon d'essence","AchatArme(hash, prix)",nil)
    Menu.addButton("Fermer","WeaMenu",nil)
end


local function AchatArme(hash, prix) --  here's the function who call GiveWeaponToPed and who call the serveur function CheckMoneyForWea

	local ped = GetPlayerPed(-1)
	wait(0)
	GiveWeaponToPed(ped, GetHashKey(hash), 1000, false)
	TriggerServerEvent('CheckMoneyForWea',hash,prix)

end

Server (copied from the essential weashop script):

RegisterServerEvent('CheckMoneyForWea')
AddEventHandler('CheckMoneyForWea', function(weapon,price)
	TriggerEvent('es:getPlayerFromId', source, function(user)

		if (tonumber(user.getDirtyMoney) >= tonumber(price)) then
			local player = user.identifier
			local nb_weapon = 0
			local executed_query = MySQL:executeQuery("SELECT * FROM user_weapons WHERE identifier = '@username'",{['@username'] = player})
			local result = MySQL:getResults(executed_query, {'weapon_model'}, "identifier")
			if result then
				for k,v in ipairs(result) do
					nb_weapon = nb_weapon + 1
				end
			end
			print(nb_weapon)
			if (tonumber(max_number_weapons) > tonumber(nb_weapon)) then
				-- Pay the shop (price)
				user.removeDirtyMoney((price))
				MySQL:executeQuery("INSERT INTO user_weapons (identifier,weapon_model,withdraw_cost) VALUES ('@username','@weapon','@cost')",
				{['@username'] = player, ['@weapon'] = weapon, ['@cost'] = (price)/cost_ratio})
				-- Trigger some client stuff
				TriggerClientEvent('FinishMoneyCheckForWea',source)
				TriggerClientEvent("es_freeroam:notify", source, "CHAR_STRETCH", 1, "Pablo", false, "Bon choix! Et en faite, tu ne m'as jamais vu.\n")
			else
				TriggerClientEvent('ToManyWeapons',source)
				TriggerClientEvent("es_freeroam:notify", source, "CHAR_STRETCH", 1, "Pablo", false, "Tu as atteinds la limite d'armes! (max: "..max_number_weapons..")\n")
			end
		else
			-- Inform the player that he needs more money
			TriggerClientEvent("es_freeroam:notify", source, "CHAR_STRETCH", 1, "Pablo", false, "T'as pas assez de thune!\n")
		end
	end)
end)

If someone could tell me what’s wrong with this please!

Have a nide day.

You should backtrack through the code, also know as “debugging”.

  • Does the native itself work? Try not putting it into a function
  • Does the AchatArme function get called?
    • if so, are the arguments valid?
    • If not, Menu.addButton callback isn’t working properly.

Try figuring it out by yourself first.

1 Like

Thank you for your answer Syntasu, i’ll try it asap and come back to give you some feedback!

I do everything step by step and thanks, i found my mistakes! I feel dumb cause i didn’t try it by myself… I was too impatient!

The arguments weren’t a the good places

function ThrowMenu()
    ClearMenu()
    options.menu_title = "Arme de lancé"
	Menu.addButton("Fumigène","AchatArme(hash, prix)",nil)
	Menu.addButton("Coctail molotov","AchatArme", {'WEAPON_Molotov' , 10000})
	Menu.addButton("Bidon d'essence","AchatArme(hash, prix)",nil)
    Menu.addButton("Fermer","WeaMenu",nil)
end


function AchatArme(arg)
	hash = arg[1]
	prix = arg[2]
	local ped = GetPlayerPed(-1)
	GiveWeaponToPed(ped, hash, 1000, 0, 0)
	TriggerServerEvent('CheckMoneyForWea',hash,prix)
	CloseMenu()
end

I used the function ‘addbutton’ wrong. the arguments need to be at the last place.

Thanks again for your answer and sorry if my english wasn’t really good ><
Cya!