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.
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.
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.
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.
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”.
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.