Hi everyone, so today I was making a utilities module to create easy access functions, but I ran into a problem.
My module tree
I started to make functions like utilities.random() and such, but I tried to optimise my code with a for loop
--Inside the "utilities" module
local utilities = {}
for i, v in pairs(script:GetChildren()) do
local name = v.Name
local funcName = "utilities"..name
funcName = function()
require(script:FindFirstChild(name))()
end
print(funcName)
end
return utilities
as you can see, I was trying to make a function with a concatenated string, as seen in funcName, then set it as a function, but after debugging I found out that the name was not saved in the function:
The initial utilities.random return in the console is funcName before setting it to a function, function: 0xaef0a86404def21f is after changing funcName into a function
Any ideas how I can achieve my goal? Any help is appreciated, thanks!
You make a table called “utilities”, which you return
when you require the module script, studio gives you this table, however you never add anything to it
--Inside the "utilities" module
local utilities = {}
for i, v in pairs(script:GetChildren()) do
local name = v.Name
local funcName = "utilities"..name
utilities[funcName] = function()
require(script:FindFirstChild(name))()
end
print(funcName)
end
return utilities
I think adding an extra set of parenthesis to the require() statement is wrong, because it calls the function that you are trying to store. Removing it would probably work though. The other issue is the utilities table would end up looking like