How shall I run Module Scripts properly?

Hey! I’ve scripted my own small framework consisting of these 2 main scripts:

Server side:

local t = tick()
for _,Module in script:GetChildren() do
	if Module:IsA("ModuleScript") then
		print("S | Trying to initizialize "..Module.Name)
		--task.spawn(function()
			require(Module)
		--end)
		warn("S | Initizialized "..Module.Name)
	end
end

print("S | Took "..(tostring(tick() - t)).." seconds to initizialize Server modules.")
print("----- INITIZIALIZED ALL MODULES WITHIN SERVER ------")

Local side:

local LocalModules = game.ReplicatedStorage:WaitForChild("LocalModules")

local t = tick()

for _,Module in LocalModules:GetChildren() do
	if Module:IsA("ModuleScript") then
		print("L | Trying to initizialize "..Module.Name)
		task.spawn(function()
			require(Module)
		end)
		warn("L | Initizialized "..Module.Name)
	end
end

print("L | Took "..(tostring(tick() - t)).." seconds to initizialize Client modules.")
print("----- INITIZIALIZED ALL MODULES WITHIN LOCAL ------")

My question is now, is it alright if I run EVERY module script in a seperate thread via task.spawn or will this cause performance issues sometime? I am running loops in the module scripts which is why I am required to do so at the moment.

It shouldn’t, but idk what all your initializing, but honestly you should look into multi-threading if you’re unsure or run into performance issues.

Well, I am initizialing a lot of main handling stuff and UI maionly. So animation handler, UI main handler, profile service, etc.

No real reason to do this… The module should be an extension of whatever script is calling it, ran by that script.

This is not a concern, if it was Walmart Corporate wouldn’t be able to use pyspark to process billions of records distributed across several clustered nodes.

But really, it does not matter.

It’s nearly identical to simply using normal scripts since you can think of different scripts as implicitly being their own task. There are good reasons to handle it the way you do though (like consistency or controlling load order). It won’t really degrade performance notably compared to other methods unless you go unrealistically crazy with it.

1 Like

The beauty of modules (as opposed to globals - _G) is that you really should not need to initialize them. Each script that requires such a module would wait for it to run fully before proceeding. It does not matter which script runs the module first, as they all basically use the same copy (actually one copy for server and one copy for each client, if you share modules this way)

Besides, if your modules yield for anything other than preloading assets, you are doing modules wrong.

1 Like

Well I am structuring my whole code base to be with modules. This way it’s more organized personally and I prefer using modulars than regular scripts.

Doesn’t that defeat the purpose of module scripts though?

I mean you do you, and there is no one “proper” way to write code, but the point of module scripts is that you can share them between scripts. Whether you use object-oriented, structured or my favourite, composition coding; modules help you avoid repeating the same code multiple times and share data between scripts.

So unless your modules require nested modules within, you get no benefits of modules this way.

The performance overhead isn’t anything to worry about, but it can make it more difficult to identify what code runs when. Personally, I don’t really know if I’d ever run into this kind of situation, I’ve only ever require’d all the Modules I use in Scripts all at once without using coroutines/task.spawn, so this could be a symptom of poor design with how your Modules work.

If you’re literally turning Scripts into the ModuleScripts, then there’s no point in this from a performance perspective. Each Script runs under a different Coroutine like you’re running the Modules with task.spawn. It just seems like extra work doing the same thing to me, but it’s your personal preference in terms of “organization”.

1 Like

This way, I can better control which scripts are being initizialized when & I have from the start up the option to use other Module Scripts for an another Script as example too.