1
votes

So I have a Lua code where I have a bunch of different functions in a file (as multiple scripts use them) and I want one to be able to execute some of them based on user input. So if the user inputs addUser() (one of my functions) then the code will run addUser(). Now, I know u can do it like

    var = io.read()
    if var == (some function) then
        (function)
    else
        if var == (some other function) then

Etc.

But I want a simpler way of doing it.

1

1 Answers

1
votes

Let the user input just the name of the function and then do

var = io.read()
if _G[var]~=nil then _G[var]() end

If you want to allow foo(), use

var = var:gsub("[%s()]","")

just after reading.