Module Loader Feedback

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
2 Likes

It’s cool, but if I was you i’d turn it into a universal module so you could use it anywhere, aswell as reduce the wait time instead of 1 second to 0.1 to avoid long pauses, unless its there with a specific reason

2 Likes

Thanks I just didn’t know how long to make the wait.

Oh lol, thought it was put there with a specific reasoning xd, also another thing I wanted to add which would be sick to see is a priority-based loader

1 Like

That’s a good idea I’ll consider it.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.