How would I call a function from a string?

I am trying to make admin but dont want to do loads of if statements im trying to get the array at the top and call the function

local Commands = {“fly”, “sm”, “ban”, “kick”, “view”, “explode”, “nuke”, “char”, “kill”, “fling”, “crash”, “heal”}

function fly()
print(“ALIEN”)
end

game.Players.PlayerAdded:Connect(function(Player)

Player.Chatted:Connect(function(Message)
	
	local Args = (Message):split(" ")
	print(Args[1])
	Args[1]() -- Call function here
	
end)

end)

2 Likes

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
}

Tables/dictionaries are really useful, and I’d recommend reading more about them here: Tables | Documentation - Roblox Creator Hub

7 Likes

eh i know its 3 years later, but i still got a question

i put the script inside a module script. how do i access the fly function with a string named ‘fly’ in another script?

1 Like

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:

  1. Put your dictionary with the “fly” function in a modulescript
  2. Return that dictionary
  3. In your script (or multiple scripts) you can require the module and use the function as if it was in each of those scripts

Modulescript:

Commands = {
    ["fly"] = function()
        print("ALIEN")
    end
}

return Commands

Script (or LocalScript):

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.

1 Like

could you do

local foo = "fly"
Commands[foo]()


image
doesnt work :huh:

Terrain isnt a dictionary/ array, its an instance.

so how to call the function in that case?

You need to inject the “self” parameter by putting “workspace.Terrain” as the first argument.

1 Like