Module Script organization help

image_2024-10-22_132009356
So I’ve been learning how use a more modular framework and I have questions regarding on how to initialize the modules in server scripts.

--Services
local RS = game:GetService("ReplicatedStorage")
local PS = game:GetService("Players")

--Modules
local AttributeModule = require(script.PlayerAttribute)
local DataManager = require(script.DataManager)
local WaveManager = require(script.WaveManager)
local DamageManager = require(script.DamageManager)

--Events
local ChooseSaveFiles = RS.Events.RemoteEvent.ChooseSaveFiles
--Variables


--Functions
local function onPlayerJoin(player)
	WaveManager.Initialize()
	DamageManager.Initialize()
end

local function onCharacterJoin(character) --non data saving values
	local Player = PS:GetPlayerFromCharacter(character)
	AttributeModule.Initialize(character)
end

local function loadSaveFile(player, SaveFile)
	DataManager.loadData(player, SaveFile)
end

local function onLeave(player)
	DataManager.saveData(player)
end

--Events	
PS.PlayerAdded:Connect(function(player)
	onPlayerJoin(player)
	player.CharacterAdded:Connect(onCharacterJoin)
end)

PS.PlayerRemoving:Connect(onLeave)

ChooseSaveFiles.OnServerEvent:Connect(loadSaveFile)

So here I’m just requiring all the modules and initializing them but should I have a parent module that initializes all the other modules when called on server scripts like Parent Module → submodule 1, submodule 2, submodule 3 etc… or are there better way to initialize modules on server scripts. Also should I put all the events in the modules instead of the server scripts?

Some people use a “Library” module script that automatically requires all ModuleScripts placed under it and returns a mapping of the ModuleScript name to the actual module.

There isn’t really a better way though, it’s just different styles.

The reason it’s all the same is because Roblox only actually “requires” a module once, and after that require just returns the first one. This means that even if you call require in every script it’s not actually doing extra work.


This depends on how you want to break up your code. Either way is fine really, some people even put all their code in a single script.

I like to break out code where possible. One tip is to have modules represent “interfaces” such that they expose only the most basic usage. For example, a data storing module might only expose “load data” and “write data” functions and do all the saving behind the scenes. This makes it simple to use.

In general I would just try not to over think it. Consistency tends to be more important, so I would pick a method and stick with it, at least for a single code base.


Also, this might be a problem here:

Initializing these for every player without specifying a player or saving the result to a player table might be unintended. Perhaps there is some logic behind the scenes to only set up the most recently added player though.

2 Likes

As the previous reply states, there is no “right way”. It is all up to preference.
For example, here’s one of my more recent works:


Yet I could also use a different structure and nothing would change. I’ve explored lots of different styles, but I seem to enjoy such structuring alongside Object-Oriented Programming.

3 Likes

Thanks, I’ll be trying out different methods

1 Like