Store Function In Table - Pass Arguments Later

I’m creating an admin addon and I’ve decided to do it as follows:

Each command has a function, the commands are stored in tables which tell the SGNAdminCommandListener hook loops through to find which function to execute for the command you used.

My question is, when I use ‘!print hello’, it prints ‘hello’ in the console. However when I use ‘!kick Semajnad’, it say’s:



[ERROR] addons/sgn_admin/lua/autorun/server/init.lua:78: attempt to call field 'func' (a nil value)
  1. v - addons/sgn_admin/lua/autorun/server/init.lua:78
   2. unknown - lua/includes/modules/hook.lua:84


[ERROR] addons/sgn_admin/lua/autorun/server/init.lua:78: attempt to call field 'func' (a nil value)
  1. v - addons/sgn_admin/lua/autorun/server/init.lua:78
   2. unknown - lua/includes/modules/hook.lua:84


Now, I know ‘!kick Semajnad’ wouldn’t work, as Semajnad is not a player object, however at the moment it doesn’t even seem to be able to find ‘SGNAdmin.Kick’ in the PlayerSay hook as ‘info.func’. However it does find ‘info.friendly’ as a string.




SGNAdmin.Commands = {
	["kick"] = {
		friendly = "Kick",
		usage = "kick [player]",
		permission = "sgn.kick",
		catagory = 1,
		func = SGNAdmin.Kick
	},
	["print"] = {
		friendly = "Print",
		usage = "print [message]",
		permission = "sgn.print",
		catagory = 1,
		func = print
	}
}

hook.Add( "PlayerSay", "SGNAdminCommandListener", function( ply, text )
	if string.StartWith( text, "!" ) then
		for cmd, info in pairs( SGNAdmin.Commands ) do
			if string.StartWith( text, "!" .. cmd ) then
				args = string.Explode( " ", text )
				print("Worked: The command used was: " .. info.friendly .. " and the caller was: " .. ply:Nick() )
				info.func( args[2] )
				break
			end
		end
	end
end )

function SGNAdmin.Kick( ply )
	ply:Kick()
end


Can someone point me in the right direction and if I’m being stupid, just say :slight_smile: I’ve never stored functions like this to pass parameters later, however I’m thinking it should be possible.

For my own troubleshooting, I’ve tried info.func = SGNAdmin.Kick, info.func = Kick, [“info.func”] = SGNAdmin.Kick etc. I’m guessing there’s something obvious I’m doing wrong :slight_smile:

Put the function for SGNAdmin.Kick() at the top.

Damn, I’ll give that a try. I moved the hook to the bottom, but didn’t think to move the table. So if I wanted the table of command separate, would I have a file with the table, but at the top ( before the table ) include the functions lua file?

[editline]17th August 2015[/editline]

@StonedPenguin Why did you disagree with Chessnut. He was right?

[editline]17th August 2015[/editline]

Thanks, just confirmed it does work :slight_smile:

You should make an “main” file, which includes all the function file, and a file for the chat check + table as last.

Ok, thanks for the advice :slight_smile: