Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is there a way to include variables inside function names?

I have been writing and admin script and I want to keep it as space-efficient as possible. What I want is for the ghostCommand function to be called and the unghostCommand function to be called. However I can't find a way to include a variable within a function name.

--Code snippet:

local commandsTble = {"*ghost","*unghost"}
local noAstCommandsTble = {}
for i = 1,#commandsTble do
	table.insert(noAstCommandsTble,i,string.sub(commandsTble[i],2,string.len(commandsTble[i])))
end

for i = 1,#commandsTble do
	if string.sub(msg,1,spaceLocation-1) == commandsTble[i] then
			noAstCommandsTble[i]Command() ** -- Here is the problem (I am including a variable        				
												-- inside of a function name)**
	end
end

function ghostCommand()
	print("Ghost command called")
end

function unghostCommand()
	print("Unghost command called")
end

This does not work, however is there a way to make it work? I do not want to write an if statement for each command if I can help it. Thanks for reading!

6 comments

0

Please use a code block.

0

I don't know what you mean

0

You can use a module with the main loader... I'm not too experienced with modules or chat commands myself so my comment may be completely wrong. Though modules do make for cleaner, faster, more maintainable code.

0

Please put your code in a code block. The lua icon at the top of the question editor.

0

sorry, added code block

0

You might be interested in studying Roblox's "vote to kick" script's "matchPlayer" function, which will work with partial matches (ex "*gh" will match "*ghost" so long as there aren't multiple things that "*gh" could match)

2 answers

I think this approach would work better:

local commands = {}
function commands["*ghost"] = function()
	print("Ghost command called")
end
function commands["*unghost"] = function()
	print("Unghost command called")
end
function commands.other = function() --I put this here to demonstrate that if you have no special characters nor spaces, you can just use  .text  instead of  ["text"]
	print("'other' command called")
end
--etc
--Another option (does exactly the same thing):
local commands = {
	["*ghost"] = function()
		print("Ghost command called")
	end,
	["*unghost"] = function()
		print("Unghost command called")
	end,
	other = function()
		print("'other' command called")
	end,
	--etc
}


--To convert from 'msg' to a function:
local action = commands[string.sub(msg,1,spaceLocation-1)]
if action then
	action()
else
	print("That was not a valid command")
end

There is a way to do exactly what you ask (call a function whose name is equal to the value of a variable) using getfenv():

function test1() print("in test1") end
function test2() print("in test2") end
v = "test2"
getfenv()[v]() --will print "in test2"

That last line works like this: getfenv() returns a table with values from the current environment in it. The next part, [v], indexes the table with the desired name (test2 in the example). The final () just invokes the function (just like the final () in test1())

........Command(vars)

calls the 'command' with vars

This discussion is preserved as it was. New answers, comments, and votes are closed.