Hi, I’m trying to make a gamemode at the moment and I’m trying to make it so that when the user presses Q, they drop their weapon. I’ve tried copying an example from a video but my problem is that the server receives the player’s name (needed to set the pos) as ‘0’ and the weapon’s name as the player’s name. I’ve spent quite a while googling and looking though my code and trying various things but to no avail. If anybody can help me I will be very happy.
Also, the code gets executed numerous times (I think this might be because of the actual IsKeyDown() function being called rapidly). Is there a better function to put here?
check for Q press (client side):
local function keyQ() if input.IsKeyDown(KEY_Q) then net.Start("KEY_Q") net.SendToServer() hook.Call("KEY_Q", GAMEMODE) end end hook.Add("Think", "q_pressed", keyQ)
Client-side weapon-dropping code:
function weaponDrop() local ply = LocalPlayer() if !IsValid(ply:GetActiveWeapon()) then return end local wep = tostring(ply:GetActiveWeapon():GetClass()) RunConsoleCommand("ent_remove", wep) net.Start("SpawnWeapon") net.SendToServer() hook.Call("SpawnWeapon", ply, wep) end hook.Add("KEY_Q", "weaponDrop", weaponDrop)
Entity creation (server side) - here’s where the crash is:
function weaponSpawn(ply, wep) --wep is userdata type and = player's name, ply is 0 local ent = ents.Create(tostring(wep)) local pos = ply:GetPos() ent:SetPos(pos + Vector(10,0,20)) end
Network receiving code (obviously server-side):
net.Receive("KEY_Q", function(len, ply) hook.Call("KEY_Q", GAMEMODE, ply) end) net.Receive("SpawnWeapon", function(ply, wep) weaponSpawn(ply, wep) end)
I’m new to lua and this is the first networking code I’ve ever come across in my life. I know it’s a little out of my reach but I’m eager to learn and any help will be greatly appreciated! I know my code is a horrible mess, mainly because I was just trying everything, so sorry about that.