Addon error "bad key to string index (number expected, got string)"

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


[ERROR] gamemodes/hrp/gamemode/init.lua:30: bad key to string index (number expected, got string)

the code that relates to this is
init.lua


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 )

and doortext.lua


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

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: http://wiki.garrysmod.com/page/Console_commands

  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:


concommand.Add( "unlockdoor",function( ply, ent )

with


function unlockdoor( ply, ent )

and


RunConsoleCommand( "unlockdoor", ply, ent )

with


unlockdoor( ply, ent )

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


[ERROR] gamemodes/hrp/gamemode/visuals/doortext.lua:18: attempt to call global 'lockdoor' (a nil value)

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


 [ERROR] gamemodes/hrp/gamemode/cl_init.lua:18: attempt to call method 'Fire' (a nil value)

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


[ERROR] gamemodes/hrp/gamemode/visuals/doortext.lua:2: attempt to call method 'GetName' (a nil value)

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


local plyname = ply:GetName()

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


ent_fire !picker unlock

The door unlocks.

Oh, I didn’t noticed that your doortext.lua is clientside. In this case you’ll have to use concommands.

Crap… any idea on how to get the ent id’s? seems like thats the best way to send it.
EDIT:
Found it…