Is it possible to use an iterator in function names?

Hello fellow developers!
I am a novice developer, although have created several games last 6 months.

Now I am developing an obby game (ESL Angels) with Collection Service.
I write 9 functions (level1, level2, …level9) in a module script
and now I am writing a server script in ServerScriptService to controll QA boards.

Here, I defined tagged models as “PSL1”, “PSL2”,…“PSL9”
and want to call corresponding functions from the module script.

I used for loops as follows.

for i = 1, 9 do
for _, each in pairs (CS:GetTagged(“PSL”…i)) do
(omitted)
local question, answers = QA_Manager4.level1()
(omitted)
end
end

I don’t know how I can change the function name: level1 for the iterator: i
I tried like, level…[i], but it didn’t work.

I appreciate someone show me the way or alternative way.

1 Like

Don’t use getfenv, it’s a bad practice and is messy and inefficient. However, it is one way to do this that some might suggest. Don’t do it. I suggest instead that you put the functions in a table and iterate over the table.

why not do

QA_Manager4["level"..i]()
3 Likes

What is getfenv? I didn’t know and searched but still not sure.
Sorry but I am a novice developer.

getfenv() accesses the environment of the script. It’s ok if you don’t understand it, since the are very few cases where you would need to use it. For your problem, do something like this

local LevelFunctions = {level1, level2...} -- put all the functions in this table
for i, v in ipairs(LevelFunctions) do
	-- your code
end 
1 Like

I tried and found it works.
I didn’t know I can use functions on a module script like an array.
Thank you so much!

Thank you for your generous reply.
I will note your code for my future use.