For this kind of thing the easiest way to achieve it is with a dictionary: a lookup table if you will.
Instead of a list of your function names, format it like this:
local Commands = {
fly = function()
print("ALIEN")
end,
etc.
}
Then you can simply access an entry in the dictionary with its key (in this case the string “fly”, which is what you want to type in chat to access it, and is just Args[1])
local func_name = Args[1]
local func = Commands[func_name]
func()
A function is just another datatype and it can be stored in a dictionary just like anything else.
There are of course other ways to format this code. You could set table indices after the initial creation of the table:
Commands = {}
Commands.fly = function()
print("ALIEN")
end
You could also set the key using square brackets:
Commands = {
["fly"] = function()
print("ALIEN")
end
}
A modulescript can return any data type, but usually people return dictionaries from them.
The internal data in a script can’t really be accessed, so I would recommend this:
Put your dictionary with the “fly” function in a modulescript
Return that dictionary
In your script (or multiple scripts) you can require the module and use the function as if it was in each of those scripts
Commands = require(workspace.ModuleScript) -- Or wherever you put the modulescript
Commands.fly() -- prints "ALIEN"
You can basically imagine that the thing you set equal to the require is the exact same as the actual thing you return from the modulescript. In this case it’s just like you created the Commands dictionary within the script.
The useful part of this is that you can have multiple scripts requiring the same module, so you don’t need to copy+paste your code in each one.