Giving a Weapon to a Player


I tried making a weapon command that would give you a pistol but it didn’t work.

AddEventHandler("chatMessage", function(s, n, m)
	local message = string.lower(m) -- This prevents it from NOT recognizing /WEAPON as the command (just in case the user has caps lock on)
	if message == "/weapon" then
		CancelEvent() -- Hide the message from user chat
		GiveWeaponToPed(s, "WEAPON_PISTOL", 1000, false, false)

It just gives me this error and gives me no pistol

Error running system event handling function for resource script: citizen:/scripting/lua/scheduler.lua:39: Failed to execute thread: sv.lua:5: attempt to call a nil value (global 'GiveWeaponToPed')
stack traceback:
        sv.lua:5: in upvalue 'handler'
        citizen:/scripting/lua/scheduler.lua:124: in function <citizen:/scripting/lua/scheduler.lua:123>
stack traceback:
        [C]: in function 'error'
        citizen:/scripting/lua/scheduler.lua:39: in field 'CreateThreadNow'
        citizen:/scripting/lua/scheduler.lua:123: in function <citizen:/scripting/lua/scheduler.lua:92>

Can somebody help? It would be much appreciated!


You need to define “s”


Would recommend using RegisterCommand instead of chatMessage Event.


This handler is on the server (assuming from the sv.lua filename). You will then need to trigger a client event to give the ped the weapon. The server can’t execute game natives. The server can only tell the client(s) (by events) what to do.


I believe this is how you use this on the client

RegisterCommand("weapon", function(args, string)
    local ped = GetPlayerPed(PlayerId())
    GiveWeaponToPed(ped, GetHashKey("WEAPON_PISTOL"), 1000, false, false)
end, false)

Error running call reference function for resource script: sv.lua:2: attempt to call a nil value (global 'PlayerId')
stack traceback:
        sv.lua:2: in local 'ref'
        citizen:/scripting/lua/scheduler.lua:277: in function <citizen:/scripting/lua/scheduler.lua:268>



Put the code he gave you in the client script.


Let me explain further. Anything to do with natives has to be done on a client script. So you have 2 options for commands. You can create the command on the client and put the weapons natives like I did above or you can create a client event to do it for you and use the server command to trigger the client event.

Which one would you prefer?


I would like a client event to do that for me but I have forgotten how to do one.

RegisterCommand("weapon", function(source, args, string)
    TriggerClientEvent("GivePedWeapon", source)
end, false)

AddEventHandler("GivePedWeapon", function()
    local weapon = GetHashKey("WEAPON_PISTOL")
    local ped = GetPlayerPed(PlayerId())
    GiveWeaponToPed(ped, weapon, 1000, false, false)


Thanks so much for explaining. I didn’t know the difference but I somehow came far without errors. Anyway, it works now. Thanks for the help! :slight_smile:


No problem. Thanks for your question.


You could make that even simpler, and just have the command part on the client too, and just put the weapon stuff inside the actual command


I asked if he wanted it all client or if he wanted server / client.

I like using server because I can add perms to it easier using my job ranks / staff ranks global tables.

I wasn’t sure what exactly he was trying to use it for. :smiley:


Can someone help me?
I want like /weapon revolver and get the revolver im a noob in this and i cant do it :sweat_smile: