• need help with lua errors
    10 replies, posted
hello everyone, i try to made a simple script , i'm beginner with lua script and i dont understand this following error ... this code is clientside code: [CODE] local ply = LocalPlayer() function DropActualGun() local actualweap = ply:GetActiveWeapon() -- <= actual error print(actualweap) --ply:DropWeapon(actualweap) end hook.Add( "Think", "DropWeapon", function() if input.IsKeyDown( KEY_G ) then DropActualGun() end end) [/CODE] and a error say : [CODE] [ERROR] addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:6: attempt to call method 'GetActiveWeapon' (a nil value) 1. DropActualGun - addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:6 [/CODE] thanks for comming responses :v:
[CODE]--ply:DropWeapon(actualweap)[/CODE] Remove the -- first, then make sure you do this [CODE]if ply:GetActiveWeapon() == nil then return end[/CODE] correct me facepunch if im wrong
it doesn't work : [CODE] local ply = LocalPlayer() function DropActualGun() local actualweap if ply:GetActiveWeapon() == nil then return end else print(ply:GetActiveWeapon()) end --print(actualweap) --ply:DropWeapon(actualweap) end[/CODE]
DO THIS [CODE]function DropActualGun() local actualweap = ply:GetActiveWeapon() if actualweap == nil then return end print(actualweap) ply:DropWeapon(actualweap) end[/CODE] DONT PUT -- BEFORE ply:DropWeapon(actualweap)
UPDATE : i get a new error i have find this on the wiki : [url]https://wiki.garrysmod.com/page/Player/GetActiveWeapon[/url] code : [CODE] function DropActualGun() print(ply:GetActiveWeapon():GetClass()) -- <= this print the exact weapon name : for the crowbar this will print "weapon_crowbar" ply:DropWeapon(ply:GetActiveWeapon():GetClass()) -- <=the error line end hook.Add( "Think", "DropWeapon", function() if input.IsKeyDown( KEY_G ) then DropActualGun() end end) [/CODE] error : [CODE] [ERROR] addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:6: attempt to call method 'DropWeapon' (a nil value) 1. DropActualGun - addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:6 [/CODE] but i have try this ... [url]https://wiki.garrysmod.com/page/Player/DropWeapon[/url]
You're likely defining ply before LocalPlayer() becomes valid. Use an InitPostEntity hook to define it. [lua]local ply hook.Add("InitPostEntity", "somehookname", function() ply = LocalPlayer() end)[/lua] (Or just use LocalPlayer() directly instead of ply) [url]http://wiki.garrysmod.com/page/Global/LocalPlayer[/url]
-snip-
finaly i have deleted " ply = LocalPlayer() " the game recognize directly " ply " as the player [editline]22nd June 2016[/editline] [QUOTE=BALIST0N;50567576] this code is clientside [/QUOTE]
Man, Im just looking at this thread, and wow bubbie hates phantom
so ? who can help me please ? i have find this on the wiki : [url]https://wiki.garrysmod.com/page/Player/GetActiveWeapon[/url] code : [CODE] function DropActualGun() print(ply:GetActiveWeapon():GetClass()) -- <= this print the exact weapon name : for the crowbar this will print "weapon_crowbar" ply:DropWeapon(ply:GetActiveWeapon():GetClass()) -- <=the error line end hook.Add( "Think", "DropWeapon", function() if input.IsKeyDown( KEY_G ) then DropActualGun() end end) [/CODE] error : [CODE] [ERROR] addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:6: attempt to call method 'DropWeapon' (a nil value) 1. DropActualGun - addons/gunmenu/lua/autorun/client/cl_gunmenu.lua:6 [/CODE] but i have try this ... [url]https://wiki.garrysmod.com/page/Player/DropWeapon[/url]
[QUOTE=BALIST0N;50567576]this code is clientside[/QUOTE] Any particular reason why you're doing this client side? It would be a lot easier if you do this server side, besides DropWeapon is a serverside method. [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/DropWeapon]Player:DropWeapon[/url] You could use 'KeyPress' instead [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/KeyPress]GM:KeyPress[/url]. You slightly have a good approach though I'd do it like this: [code] hook.Add( "KeyPress", "drop_weapon", function( ply, key ) if ( key == KEY_G ) then ply:DropWeapon(ply:GetActiveWeapon():GetClass()) end end ) [/code] [b] EDIT: I just noticed that this won't work for the key G, you could use another key from this [url="https://wiki.garrysmod.com/page/Enums/IN"]list[/url] [/b] Though if you really want the key G to work then this will involve a lot more: DropWeapon will only work on serverside You will need to trigger DropActualGun procedure to work with the clientside function input.IsKeyDown [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/input/IsKeyDown]input.IsKeyDown[/url] You could use the net library or use easier ways like concommands This could work, however this code would have to be serverside [code] concommand.Add( "DropActualGun", function(ply) print(ply:GetActiveWeapon():GetClass()) -- <= this print the exact weapon name : for the crowbar this will print "weapon_crowbar" ply:DropWeapon(ply:GetActiveWeapon():GetClass()) -- <=the error line end) [/code] As for clientside code it would be something like this: [code] hook.Add( "Think", "DropWeapon", function() if input.IsKeyDown( KEY_G ) then LocalPlayer():ConCommand("DropActualGun") end end) [/code]
Sorry, you need to Log In to post a reply to this thread.