Giving a Weapon to a Player


#1

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)
	end
end)

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!


#2

You need to define “s”


#3

Would recommend using RegisterCommand instead of chatMessage Event.


#4

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.


#5

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)

#6
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>

?


#7

Put the code he gave you in the client script.


#8

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?


#9

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


#10
--[[ SERVER SCRIPT ]]--
RegisterCommand("weapon", function(source, args, string)
    TriggerClientEvent("GivePedWeapon", source)
end, false)

--[[ CLIENT SCRIPT ]]--
RegisterNetEvent("GivePedWeapon")
AddEventHandler("GivePedWeapon", function()
    local weapon = GetHashKey("WEAPON_PISTOL")
    local ped = GetPlayerPed(PlayerId())
    GiveWeaponToPed(ped, weapon, 1000, false, false)
end)

#11

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:


#12

No problem. Thanks for your question.


#13

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


#14

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:


#16

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: