Hi there, I’ve made a module loader and I would like to get some feedback and how I could improve it. So far it has 3 functions. My favourite function is FireFunction, it basically fires function within the cached modules that matches the one given in the parameters. For example from one script I can have an InputBegan event and automatically call all other functions within the cached modules that have an InputBegan function. All the other functions are pretty self explainatory. Thanks for reading
Code:
-----------------------------
-- VARIABLES --
-----------------------------
local ModuleLoader = {}
local CachedModules = {}
-----------------------------
-- FUNCTIONS --
-----------------------------
function ModuleLoader.Init(scripts : {ModuleScript})
for _, module: ModuleScript in scripts do
if not module:IsA("ModuleScript") then continue end
local ModuleScript = require(module)
CachedModules[module.Name] = ModuleScript
if ModuleScript.Start then
task.spawn(ModuleScript.Start)
end
end
end
-- Gets a module within the cached modules
function ModuleLoader.Get(name: string)
local Timer = 0
if not CachedModules[name] then
repeat
task.wait(1)
Timer += 1
if Timer > 5 then
warn("Module took a long time to load, you should investigate this man", name, CachedModules)
end
until CachedModules[name] ~= nil
end
return CachedModules[name]
end
-- Looks for a function within the given name in all of the cached modules and fires it.
function ModuleLoader.FireFunction(functionName, ...)
for _, module in CachedModules do
if module[functionName] then
task.spawn(module[functionName], ...)
end
end
end
return ModuleLoader