• Addon error "bad key to string index (number expected, got string)"
    5 replies, posted
I am coding a door ownership plugin for my gamemode, however for some reason whenever I try to lock/unlock a door i get the error [CODE][ERROR] gamemodes/hrp/gamemode/init.lua:30: bad key to string index (number expected, got string)[/CODE] the code that relates to this is init.lua [CODE]concommand.Add( "unlockdoor",function( ply, ent ) local plyname = ply:GetName() local entowner = ent:GetOwner() local entownername = entowner:GetName() if plyname == entownername then ent:Fire( "unlock" ) end end ) concommand.Add( "lockdoor",function( ply, ent ) local entowner = ent:GetOwner() local entownername = entowner:GetName() local plyname = ply:GetName() if plyname == entownername then ent:Fire( "lock" ) end end )[/CODE] and doortext.lua [CODE]ply = LocalPlayer() plyname = ply:GetName() function decdoortext() ent = LocalPlayer():GetEyeTrace().Entity plrvec = LocalPlayer():GetPos() entvec = ent:GetPos() if ent:GetClass() == "prop_door_rotating" and entvec:Distance( plrvec ) < 150 then lookingatdoor = true if ent:GetOwner().Nick != nil then entowner = ent:GetOwner() entownername = entowner:GetName() DoorText = "Door Owned By "..entowner:GetName() if entownername == plyname then DoorText1 = "Press X to Lock/Unlock Door" DoorText2 = "Double tap X to Sell Door" else DoorText1 = "" DoorText2 = "" end else DoorText = "Door Not Owned, Press X to buy" if input.IsKeyDown( KEY_X ) then ent:SetOwner( LocalPlayer() ) print( plyname.." Is Owning A Door" ) RunConsoleCommand( "unlockdoor", ply, ent ) end end else DoorText = "" DoorText1 = "" DoorText2 = "" end end[/CODE] I have also tried putting the code from the init.lua in the shared.lua and the cl_init.lua also, the doortext.lua is included from the cl_init.lua Thanks in advance
Oh god. Lol. 1) concommand.Add function has 3 arguments and they are not ( ply, ent ) They are ( ply, command, arguments ) or ( ply, cmd, args ) for short. Read: [url]http://wiki.garrysmod.com/page/Console_commands[/url] 2) RunConsoleCommand does not allow any arguments besides strings. So, things like RunConsoleCommand( "unlockdoor", ply, ent ) won't work. You'll have to use something like RunConsoleCommand( "unlockdoor", ply:UniqueID(), ent:EntIndex() ) and retrieve those values from args variable of concommand.Add and cycle through all players and find the one that matches the unique ID and get the entity as Entity(args[2]) You can probably do the same for player instead of UniqueID by the way. [editline]28th September 2013[/editline] Also you did not indicate what like is 30, so I can't help you with the actual error. [editline]28th September 2013[/editline] Also, why are you using console commands when you can simply just use functions? Like replace: [code]concommand.Add( "unlockdoor",function( ply, ent )[/code]with [code]function unlockdoor( ply, ent )[/code] and [code]RunConsoleCommand( "unlockdoor", ply, ent )[/code] with [code]unlockdoor( ply, ent )[/code]
[QUOTE=Robotboy655;42335819]Oh god. Lol. 1) concommand.Add function has 3 arguments and they are not ( ply, ent ) They are ( ply, command, arguments ) or ( ply, cmd, args ) for short. Read: [url]http://wiki.garrysmod.com/page/Console_commands[/url] 2) RunConsoleCommand does not allow any arguments besides strings. So, things like RunConsoleCommand( "unlockdoor", ply, ent ) won't work. You'll have to use something like RunConsoleCommand( "unlockdoor", ply:UniqueID(), ent:EntIndex() ) and retrieve those values from args variable of concommand.Add and cycle through all players and find the one that matches the unique ID and get the entity as Entity(args[2]) You can probably do the same for player instead of UniqueID by the way. [editline]28th September 2013[/editline] Also you did not indicate what like is 30, so I can't help you with the actual error. [editline]28th September 2013[/editline] Also, why are you using console commands when you can simply just use functions? Like replace: [code]concommand.Add( "unlockdoor",function( ply, ent )[/code]with [code]function unlockdoor( ply, ent )[/code] and [code]RunConsoleCommand( "unlockdoor", ply, ent )[/code] with [code]unlockdoor( ply, ent )[/code][/QUOTE] Thanks for helping, I was using the concommand.Add because I have NFI how to get the function called in init.lua into the doortext.lua (or even the cl_init.lua ). using the function did fix the original error however when I put the functions where they should be ( I think ), in the init.lua it starts saying that [CODE][ERROR] gamemodes/hrp/gamemode/visuals/doortext.lua:18: attempt to call global 'lockdoor' (a nil value)[/CODE] and I am pretty sure that means that the function cant be found ( is nil ). and when I try to put the functions inside the cl_init.lua ( I had to try, alright... ) I get a fire error [CODE] [ERROR] gamemodes/hrp/gamemode/cl_init.lua:18: attempt to call method 'Fire' (a nil value)[/CODE] However, as the wiki states, the Fire method is for a server to run, not the client. Oh and cl_init.lua has **** all in it, just some includes and a derivegamemode Also, for some strange reason I get this on startup [CODE][ERROR] gamemodes/hrp/gamemode/visuals/doortext.lua:2: attempt to call method 'GetName' (a nil value)[/CODE] however once I update that script it loads fine. I think it has something to do with the player not existing yet, as I get it when loading... And for the OP, line 30 is this[CODE]local plyname = ply:GetName()[/CODE] Thanks in advance EDIT: I now know that the doors can be unlocked with ent:Fire( "lock" ) because when I look at them and type in console [CODE]ent_fire !picker unlock[/CODE] The door unlocks.
Oh, I didn't noticed that your doortext.lua is clientside. In this case you'll have to use concommands.
[QUOTE=Robotboy655;42343325]Oh, I didn't noticed that your doortext.lua is clientside. In this case you'll have to use concommands.[/QUOTE] Crap... any idea on how to get the ent id's? seems like thats the best way to send it. EDIT: Found it...
[QUOTE=Hydrax;42343512]Crap... any idea on how to get the ent id's? seems like thats the best way to send it.[/QUOTE] [url]http://wiki.garrysmod.com/page/Entity/EntIndex[/url]
Sorry, you need to Log In to post a reply to this thread.