Is it possible to run a function with a string

I am wondering if it’s possible to run a function using a string, something like this as an example:

function test()
   print("worked")
end

-- Wrong Way:
"test"()

-- Right Way:
test()

This would be very useful to me to create a system that will run a specific function whenever I want it to. Heres an example of what I tried to do:

-- This is WRONG and throws 16:14:32.218 - Workspace.Script:8: attempt to call a string value
function test()
	print("hello")
end

function runFunction(key)
	key() 
end

runFunction("test")

I realize it is possible to do something like this by using if statements, But it isn’t as practical:

-- This works.
function test()
	print("hello")
end

function runFunction(key)
	if key == "test" then test() end
  --if key == "test2" then test2() end
  -- on and on...
end

runFunction("test")

So, is something like this even possible? Thanks.

7 Likes

I’ve asked this question before and the only responses I received was no. You’re going to have to do the if statement unfortunately. I’ve looked up if it was possible to do string to argument in lua but I wasn’t able to find a way to do it.

3 Likes

Oh for real? Darn. Thanks for the response. Im gonna wait a little while longer to receive more replies.

1 Like
function test()
    print("hello")
end


local thing = getfenv()


thing["test"]()

about the closest thing to what you are saying that i can think of. not very good because of having to declare a bunch of global functions

2 Likes

You can, with a bit of table usage:

local FunctionTable = {}
local String = "Foo"

FunctionTable.Foo = function() 
  print("Hello") 
end

FunctionTable[String]() -- Prints "Hello"

@ozyubkx’s solution can work but it’s not as efficient.

16 Likes

I don’t see the point of this, can’t you just call the function directly?

You may want to do this for admin commands, maybe?

1 Like

Im going to have an event purchasing system. Players use a RemoteEvent to pass to the server what event they want to purchase. Lets say billy purchases the “gun” event, so the server script is going to run the function called “gun”. Inside of the gun function, it handles giving every player the gun. This is my plan for using this.

1 Like

You should not be doing this since exploiter can potentially pass any string over the remote event, instead just make a key and then make a dictionary that maps the key to function. That way you can verify the key by checking if it exist like this:

local Functions = {
    Gun = function()
    end
}

SomeRemote.OnServerEvent:Connect(function(Player, Key, ...)
   local Function = Functions[Key]
   if Function then
       Function(...)
   end
end)
1 Like

I left out the part where I was going to verify the purchase. I was leaning towards making the if key == "test" then test() end so this wouldnt really have been an issue.

1 Like

loadstring if you enable it in the properties of ServerScriptService and do it on the server

Then why not just use my method, what your requesting is not possible and you have two options: my method or @ozyubkx’s method. Your over complicating a simple issue.

You can get pretty close to the desired effect of this using module scripts.

If you have a module, fancyModule, you can call fancyModule [ “functionName” ] () and it will execute with whatever you pass it.

This is only 1 of the endless advantages of modular coding. If you aren’t already coding modularly, you should start: like now.

Here: Writing Modular Code - A Noob's Guide

(Full disclosure, I haven’t read that tutorial, but it’s highly upvoted so I assume it’s decent enough.)

Same thing as what I am doing, since you will be implementing the module script as a table.

Sort of off-topic, but this doesn’t really seem like an advantage of modular coding. Modular code is more about splitting up your code into reusable, self-contained components. This is more of a feature of Lua tables.

2 Likes

Yeah, I was actually planning on having the entire thing in a module to just get it out of the way so my MasterScript is easier to look at. Happy birthday!

Yea, the if key == "test" then test() end example I used in the initial post actually does work. Besides that, I will probably use ideas from your method and @ozyubkx’s method to get want I want. Thanks.

It’s not the MAIN advantage of modular coding, which, as you said, is breaking up code.

It is still an advantage though. Much cleaner than having a table with a bunch of random functions in it.

1 Like