• [ERROR] lua/giverandom.lua:205: attempt to call method 'Give' (a nil value) 1. unknown - lua/giver
    20 replies, posted
So basically, I am trying to make a command that gives me a random gun from a predefined list, but whenever I run it, it gives me this error: [LUA] [ERROR] lua/giverandom.lua:205: attempt to call method 'Give' (a nil value) 1. unknown - lua/giverandom.lua:205 2. unknown - lua/includes/modules/concommand.lua:54 [/LUA] This is my code: [LUA] if CLIENT then RunConsoleCommand("GiveMeAGun") end -- serverside if SERVER then concommand.Add("GiveMeAGun", function(sender, command, arguments) if not sender:IsValid() then return end recieveditem = table.Random(list) sender:Give(recieveditem) end) end [/LUA]
Post the table you are trying to give from.
[QUOTE=Tobiasx;50755418]Post the table you are trying to give from.[/QUOTE] it's just a list of SWEPs from an addon.
Its a bad idea to use "list" as a variable when its a category/function. Change "list" to something like "_list" and use print messages to debug the code [code] if CLIENT then -- client print("Running CMD on client") RunConsoleCommand("GiveMeAGun") else -- serverside print("Code ran") concommand.Add("GiveMeAGun", function(sender, command, arguments) if not sender:IsValid() then print("Invalid sender") return end recieveditem = table.Random(_list) if !recieveditem then print("recieveditem is nil for some reason. The '_list' is a "..type(_list)) else print("recieveditem is",recieveditem) end sender:Give(recieveditem) end) end[/code]
[QUOTE=Nak;50756236]Its a bad idea to use "list" as a variable when its a category/function. Change "list" to something like "_list" and use print messages to debug the code [code] if CLIENT then -- client print("Running CMD on client") RunConsoleCommand("GiveMeAGun") else -- serverside print("Code ran") concommand.Add("GiveMeAGun", function(sender, command, arguments) if not sender:IsValid() then print("Invalid sender") return end recieveditem = table.Random(_list) if !recieveditem then print("recieveditem is nil for some reason. The '_list' is a "..type(_list)) else print("recieveditem is",recieveditem) end sender:Give(recieveditem) end) end[/code][/QUOTE] Gives me this error: [LUA] [ERROR] lua/giverandom.lua:208: bad argument #1 to 'Give' (string expected, got table) 1. Give - [C]:-1 2. unknown - lua/giverandom.lua:208 3. unknown - lua/includes/modules/concommand.lua:54 [/LUA]
[QUOTE=obesewhale;50758811]Gives me this error: [LUA] [ERROR] lua/giverandom.lua:208: bad argument #1 to 'Give' (string expected, got table) 1. Give - [C]:-1 2. unknown - lua/giverandom.lua:208 3. unknown - lua/includes/modules/concommand.lua:54 [/LUA][/QUOTE] How do you store the guns in the _list variable? [editline]23rd July 2016[/editline] Beacouse that may be the problem... It seems like you are storing tables inside a table. [editline]23rd July 2016[/editline] And that is made to work with strings inside a table.
[QUOTE=geferon;50758827]How do you store the guns in the _list variable? [editline]23rd July 2016[/editline] Beacouse that may be the problem... It seems like you are storing tables inside a table. [editline]23rd July 2016[/editline] And that is made to work with strings inside a table.[/QUOTE] I fixed it. Also, I do have a question. How would I make it so it stripped the old weapon when it gave you the new one?
[QUOTE=obesewhale;50758963]I fixed it. Also, I do have a question. How would I make it so it stripped the old weapon when it gave you the new one?[/QUOTE] I guess you should strip the old weapon with this weapons hook: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/Equip]SWEP:Equip[/url]
[QUOTE=geferon;50759060]I guess you should strip the old weapon with this weapons hook: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/WEAPON/Equip]SWEP:Equip[/url][/QUOTE] Why not just use [URL="http://wiki.garrysmod.com/page/Player/StripWeapon"]Player/StripWeapon[/URL]?
[QUOTE=Liquidsocks;50760596]Why not just use [URL="http://wiki.garrysmod.com/page/Player/StripWeapon"]Player/StripWeapon[/URL]?[/QUOTE] What i meant is using that function inside the weapons SWEP:Equip... Read before you post please.
[QUOTE=geferon;50761436]What i meant is using that function inside the weapons SWEP:Equip... Read before you post please.[/QUOTE] wouldn't using "SWEP:Equip" require you to edit the SWEP's code? Because that would be very annoying, and also not what I am looking for.
[QUOTE=obesewhale;50762110]wouldn't using "SWEP:Equip" require you to edit the SWEP's code? Because that would be very annoying, and also not what I am looking for.[/QUOTE] Oh, i thought you were creating a swep. Then you should use this hook: [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/PlayerCanPickupWeapon]GM:PlayerCanPickupWeapon[/url]
[QUOTE=geferon;50761436]What i meant is using that function inside the weapons SWEP:Equip... Read before you post please.[/QUOTE] I actually suggested OP should literally just call [URL="http://wiki.garrysmod.com/page/Player/StripWeapon"]Player/StripWeapon[/URL] after he gives the player the new weapon. But have fun messing with hooks to strip a weapon :what:
[QUOTE=Liquidsocks;50766132]I actually suggested OP should literally just call [URL="http://wiki.garrysmod.com/page/Player/StripWeapon"]Player/StripWeapon[/URL] after he gives the player the new weapon. But have fun messing with hooks to strip a weapon :what:[/QUOTE] Or that as well... Or maybe he wants it as well so if someone picks it from the ground it strips as well?
[QUOTE=geferon;50766715]Or that as well... Or maybe he wants it as well so if someone picks it from the ground it strips as well?[/QUOTE] OP said this: "How would I make it so it stripped the old weapon when it gave you the new one?". He didn't say anything about picking up weapons from the ground
[QUOTE=Liquidsocks;50766896]OP said this: "How would I make it so it stripped the old weapon when it gave you the new one?". He didn't say anything about picking up weapons from the ground[/QUOTE] Yeah, but it is what i thought. [editline]24th July 2016[/editline] So basically its what you said, strip the other weapon when giving the new one.
[QUOTE=geferon;50766977]Yeah, but it is what i thought. [editline]24th July 2016[/editline] So basically its what you said, strip the other weapon when giving the new one.[/QUOTE] yes.
With weapon strip : [code] if CLIENT then -- client print("Running CMD on client") RunConsoleCommand("GiveMeAGun") else -- serverside print("Code ran") concommand.Add("GiveMeAGun", function(sender, command, arguments) if not sender:IsValid() then print("Invalid sender") return end recieveditem = table.Random(_list) if !recieveditem then print("recieveditem is nil for some reason. The '_list' is a "..type(_list)) else print("recieveditem is",recieveditem) end local oWeap = sender:GetActiveWeapon():GetClass() sender:StripWeapon( oWeap ) sender:Give(recieveditem) end) end [/code]
What is _list?
[QUOTE=MPan1;50770400]What is _list?[/QUOTE] I assume it is his weapons table, read some posts below
the problem is running the console command on runtime clientside. when that happens, the player is not valid, so the Give function doesn't exist. you should try running it in a timer or even through your own console. edit: appears you already fixed that, sorry.
Sorry, you need to Log In to post a reply to this thread.