• DropWeapon() function not working ....
    11 replies, posted
hello everyone ! i dont understand why the dropweapon function doesn't work .... this is a clientside script i have this code : [CODE]-------------------------------------------------- --code for drop weapon script : hook.Add( "Think", "Droptheweap", function() if input.IsKeyDown( KEY_G ) then DropActualGun() end end) function DropActualGun() local weap = ply:GetActiveWeapon():GetClass() print (weap) -- print succesfully the weap name ply:DropWeapon(weap) -- <= actual error line end -------------------------------------------------[/CODE] and this error : [CODE][ERROR] addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:38: attempt to call method 'DropWeapon' (a nil value) 1. DropActualGun - addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:38[/CODE] thanks for incomming responses ! :v:
DropWeapon() is a server only function.
[QUOTE=Moosicorn;50568239]DropWeapon() is a server only function.[/QUOTE] i have tried to migrate code to server side like this : cl_gunmenu.lua [CODE]-------------------------------------------------- --code for drop weapon script : include("../server/sv_dropweap.lua") hook.Add( "Think", "Droptheweap", function() if input.IsKeyDown( KEY_G ) then DropActualGun() end end) ----------[/CODE] sv_dropweap.lua [CODE] function DropActualGun() local weap = ply:GetActiveWeapon():GetClass() print (weap) ply:DropWeapon("weapon_crowbar") end [/CODE] but he doesnt work ... :/ [CODE]weapon_crowbar [ERROR] addons/gunmenu/lua/autorun/client/../server/sv_dropweap.lua:6: attempt to call method 'DropWeapon' (a nil value) 1. DropActualGun - addons/gunmenu/lua/autorun/client/../server/sv_dropweap.lua:6 [/CODE]
The client realm and the server realm are separate. You cannot call something on one from the other without going through a library like the net library or the umsg library. You will want to do something like this instead. [lua] -- Client hook.Add( "Think", "Droptheweap", function() if input.IsKeyDown( KEY_G ) then net.Start( "dropactualgun" ) net.SendToServer() end end) [/lua] This will send a message to the server when you think the player wants to drop their weapon. I would advise against using the Think hook for this however. [lua] -- Server util.AddNetworkString( "dropactualgun" ) net.Receive( "dropactualgun", function(ln, pl) local weap = pl:GetActiveWeapon():GetClass() print( weap ) pl:DropWeapon(weapon_entity) end) [/lua] This will listen for the message being sent from the client, and when it receives it, calls the 2nd argument as a callback with 2 parameters: The length of the message (not really used for much), and the player who sent the message. We can use the latter to know who wants to drop their weapon, and then do just that.
ehm ... i dont know what that means : [CODE] [ERROR] addons/gunmenu/lua/autorun/server/sv_dropweap.lua:7: Tried to use a NULL entity! 1. DropWeapon - [C]:-1 2. func - addons/gunmenu/lua/autorun/server/sv_dropweap.lua:7 3. unknown - lua/includes/extensions/net.lua:32 [/CODE]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/DropWeapon]Player:DropWeapon[/url] takes a weapon (entity) as an argument, not the name.
lmao so the error come again but the weapon is dropped ! thats a very good start ! also, i can drop 1000 weapons per second, i need to find a way to timer the hook have you got any ideas ? PS: i have juste remove " :GetClass() " from " local weap = pl:GetActiveWeapon():GetClass() "
[code]function DropActualGun() local weap = ply:GetActiveWeapon():GetClass() print (weap) ply:DropWeapon("weapon_crowbar") end [/code] Did you guys really miss this very obvious clue: ply is undefined.
[QUOTE=Robotboy655;50568501] ply is undefined.[/QUOTE] i'm not sure :) :v::what: [editline]22nd June 2016[/editline] i have fix the script bug : caused beceause game try to drop a weapon but player have not weapon : * so this is beatifull [CODE] util.AddNetworkString( "dropactualgun" ) net.Receive( "dropactualgun", function(ln, pl) local weap = pl:GetActiveWeapon() if weap != NULL then pl:DropWeapon(weap) end end [/CODE] need to find a timer for slow down the drop rate
for the delay do this [CODE]net.Receive( "dropactualgun", function(ln, pl) timer.Create("dropDelay",2,1, function() local weap = pl:GetActiveWeapon() pl:DropWeapon(weap) end) end)[/CODE] and it doesn't drop every weapon you have, only your active one
[QUOTE=BALIST0N;50568536]i'm not sure :) :v::what: [editline]22nd June 2016[/editline] i have fix the script bug : caused beceause game try to drop a weapon but player have not weapon : * so this is beatifull [CODE] util.AddNetworkString( "dropactualgun" ) net.Receive( "dropactualgun", function(ln, pl) local weap = pl:GetActiveWeapon() if weap != NULL then pl:DropWeapon(weap) end end [/CODE] need to find a timer for slow down the drop rate[/QUOTE] This is caused by using the think hook. You could add a cooldown per player.
Thanks all of you ! i have learn much better than the gmod wiki this is the final script : cl_dropweap.lua (client side) [CODE]-------------------------------------------------- --code for drop weapon script : local droppable = true hook.Add( "Think", "Droptheweap", function() if droppable == true then if input.IsKeyDown( KEY_G ) then net.Start( "dropactualgun" ) net.SendToServer() droppable = false --the player cannot drop weapon timer.Simple( 0.5, function() droppable = true end ) --unil 0.5 seconds end end end) -------------------------------------------------[/CODE] sv_dropweap.lua (server side)[CODE]-- Server util.AddNetworkString( "dropactualgun" ) net.Receive( "dropactualgun", function(ln, pl) local weap = pl:GetActiveWeapon() --get the weapon object if weap != NULL then -- if the player have a weapon on his invetory pl:DropWeapon(weap) end end) [/CODE]
Sorry, you need to Log In to post a reply to this thread.