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

Is there any way to call a function by a string using a table? [SOLVED]

So I'm making my own admin commands and I want it to be fully customizable by other players. They can add their own commands, remove them or edit them.

The commands execute on a string by which the player chatted with the .Chatted event like most admin commands do anyway.

My plan was thinking of was when the player chats something the script searches through the "table of commands" and see if its right and has a function attached to it.

I'm not sure on if you can have a function attached to a string in a table like this:

local commands = {
	"kick" = function CommandsKickPlayer(player)
		player:Kick()
	end
}

LocalPlayer.Chatted:Connect(function(text)
	for i,v in pairs(commands) do
		if v == text then
	 		v() -- call the corresponding function
		end
	end
end)

I don't think you could do it but if there's another way on how to make easy customizable commands then please tell me.

5 comments

0

maybe loadstring can somehow be a use to this, ill see if I could look into this some more later

0

Isn't loadstring really dangerous or something? I'm not sure what it does but people tell me exploiters can come in your game and copy it. Is there anyway to make commands that are string based?

0

can't u just remove the quotation marks and just try it?

0

Are you trying to take in command functions through chat or just want commands to be used through chat?

0

I want commands to be used through chat and removing the quotation marks will not do anything because if an admin said "kill" in string format the command is going to be kill in code format and they are not the same. Only if you could convert strings to code that would be a possibility

2 answers

You can definitely have string-function pairs in tables, intact, that is primarily what modules use! Looks like you have the basic idea but have some small problems here and there:

  1. If you’re defining functions inside the table itself, you’ll have to use anonymous functions and these functions do not have any name:
-- Incorrect usage of anonymous functions
delay(1, function Foo()
    -- ...
end)

-- Correct usage of anonymous functions, names are omitted 
delay(1, function() 
    -- ...
end)
  1. This one is probably just an oversight but on line 9, you are comparing the function instead of the name: i
if v == text then

Another thing worth mentioning is that the loop is a rather odd way of checking for the command, you can simply check if the value for the index is present:

if commands[text] then
    commands[text]() 
end

6 comments

0

-1 for "If you’re defining functions inside the table itself, you’ll have to use anonymous functions and these functions do not have any name". This gives the implication that functions can have names in Lua which they cannot. Oh and the values don't need to be literals (NOT anonymous, all datatypes are anonymous, including functions), v = { x = print } works fine.

0

inb4 you say function x() end means the name of the function is x. This is syntax sugar for x = function() end. It's a variable.

0

It does imply that functions can have a name but also assumes that you already know that conventional function declarations are, as you have so generously pointed out, syntactic sugar for conventional variable declarations, and hence we are talking about the name of the variable which refers to the function.

0

Moreover the pil, along with mentioning functions don’t have names, also mentions that “When we talk about a function name, say print, we are actually talking about a variable that holds that function”. If one is not to say functions have names, why mention this? You can very much say functions have names and assume that the other understands it is the variable you’re talking about.

0

And to DEFINE a function within a table, it MUST be an anonymous function. Your print example doesn’t have print being declared in the table, simply passing the reference to a preexisting function, which is not declaration.

0

We call the result of such function constructors an anonymous function. Although we usually assign functions to global names, giving them something like a name, there are several occasions when functions remain anonymous. You seem to simply ignore the scope in which terms are being used, throwing out terms while the language itself speaks about in detail to permit its use!

I've found an plausible solution, write an anonymous function into a table into a dictionary and then execute it:

local commands = {
	["/kick"] = {
		function()
			print("I kicked a player")
		end
	}
}

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