How can I convert strings to function names?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am developing a GUI learning system and want to call functions from corresponding module scripts. For that, I let a user select subject, item and level (for example, Math, Addition, Level1) on the first screen to require the corresponding module script once a user presses
[Start] button.

  1. What is the issue? Include screenshots / videos if possible!

I searched the dev forum and found how to change strings to instances,
but I don’t know how to convert strings to function names.

I will appreciate if someone help me.

Create a table, and put the string in said table, next assign a function to the string.

local Table = {
[‘Something’] = function()
end;
};

2 Likes

Thank you for your quick reply.
Do you mean I should add following script into each module script?

levels = {
[“Level1”] = Level1(),
[“Level2”] = Level2(),

[“Level5”] = Level5()
}

How ever your scripts are layer out (if it needs to be in each script then so be it), but what u would do is create a designated module for this and call the functions when needed from the server or remote event < local script.

Also if my answer was the solution for you, you should mark is as the solution.

local levels = {}

function levels.Levels1()
	return
end

function levels:Levels2()
	return
end

return levels

This is the standard way of storing functions in module scripts.

I assume this is what you’re trying to achieve.

local levels = {}

levels["Levels1"] = function()
	return
end

levels["Levels2"] = function()
	return
end

return levels
1 Like

and I personally would lay it out as such:

local table = {

[‘index1’] = function()
return print(“hello”)
end;

end;

and not:
local table = {
[‘index1’] = index1()
end

Plus in this the latter it’s calling the function so index1 doesn’t become a function, but instead whatever index1 (function) returns.

1 Like

Thank you very much for your code.

As you can see on the right side of the second picture,
I named Addition for a module script and the folder hierarchy is:
Replicated Storage > Course Selection > Math > Addition
Because I will create other modules named as Subtraction, Multiplication, etc. later,
I need to name like this.

In this module, I want to store functions from Level1() to Level5(), returning question formulae and option answers set.
So according to your format, I will write

local Addition = {}

Addition [“Level1”] = function ()
return
end

Addition [“Level2”] = function ()
return
end

return Addition

Yes, your writing way looks more concise.
It is better if I can find more simple way to script with for example, for loop.

I changed Addition module script according to your format.
And I have a problem in the local script as you can see the red line.

I don’t know why I have problem here.

Good also please mark a solution, thanks!

My problem still persists.
Can you please look at the red line part on above picture and tell me what is a problem?

If you hover on the red line, what error do you see?

1 Like

Thank you for your reply.
Here I attach the message.

“W015: Assigning 1 values to 2 variables initializes extra variables with nil;
add ‘nil’ to value list to silence”

Basically it means that you are initializing 2 variables to 1 value.

local variable = value

Since the variable question already has the value Addition[“Level1”], answers doesn’t have a value (aka a value of nil).
‘add ‘nil’ to value list to silence’ means that you just have to add a nil after that to get rid of the error

local variable1, variable2 = value1, value2 --1 comma on each side

If you want them to have the same value you can either do

local question, answers = Addition["Level1"], Addition["Level1"]

or

local question = Addition["Level1"]
local answers = Addition["Level1"]
1 Like

In the module script, I return two variables as you can see below:

Before I used another format for functions in Module Script,
which is:

local function Addition.Level1()

return question, answers
end

In that case, I didn’t see this kind of errors.
Is this caused by the scripting format of the functions?

question is string variable (such as “1 + 1 =”) and answers is array (such as {1,2,4,7}.
And so they should not be same.

Maybe you can return it like this

return {Addition.question, Addition.answers}

and initialize the variables like

local question, answers = (Addition["Level1"])[1], (Addition["Level1"])[2]
1 Like

Thanks to you, the red line disappeared.
However, whenever I test, I get the following error message on Line 183.

Try

local A = Addition["Level1"]
local question, answers = A[1], A[2]
1 Like