Module Script System

So I am trying to make a easy to use module system as in I can sort modules, name them what I want and organize them how I please. I think I have it working now and its similar to Quenty’s code of “Nevermore” just a little more simple. Anyway this allows to me run any module in the by:

local require = require(game.ReplicatedStorage:WaitForChild("Vast"))

local Format = require("Format")

local Particles = require("Particles")

Here is the code that makes it all happen.

local RunService = game:GetService("RunService")
local ServerModules = {}
local ClientModules = {}


function AddToTable(tab, location, s)
	for i,v in pairs(location:GetDescendants()) do
		if v.ClassName == "ModuleScript" then

			local mod = v
			if tab == ClientModules then
				mod = v:Clone()
				mod.Parent = script
			elseif s then
				local name = mod.Name
				mod:Destroy()
				mod = script[name]
			end
			
			tab[v.Name] = v
		end
	end
end

local function LoadModule(name, x)
	if #ServerModules == 0 and RunService:IsServer() then
		AddToTable(ServerModules, game.ServerScriptService.Vast.Main, true)
		AddToTable(ServerModules, game.ServerScriptService.Vast.Server)
	elseif #ClientModules == 0 and RunService:IsClient() then
		AddToTable(ClientModules, script)

	end
	if RunService:IsServer() then
		return require(ServerModules[name])
	else
		return require(ClientModules[name])
	end
end

return LoadModule

Please let me know if this would be laggy or bad in anyway. It works perfectly from what I can see but I want to make sure it’s fine before adding it into my games.

2 Likes

If the module is easy to use, can you post a link to an example place so that it can be reviewed?

That code looks good enough. It’s hard to determine if this would actually be bad in production as a barebones file has not been provided that has a basic repro of your framework in action, however.

The thing that concerns me is that the writing of this module may come across as slightly messy or potentially hard to maintain. It does look like Nevermore, except the primary repository ends up in ReplicatedStorage so mostly all code is available on both environments after it’s required for the first time at run time.

Aside from the AddToTable functionality which may just be me thinking about my own paradigms for code above all, there doesn’t look like there’d be any issues in production. I say you should go ahead and use it, then revise your loader if you experience any issues.

1 Like