Developing with mostly module scripts

So for my latest projects, ive been trying to get used with scripting only with module scripts, one server script and one local/client script. I like the idea of it, but im not sure if im using it properly so I have some questions that maybe someone is willing to answer.

I also tried to avoid making this post, and instead search for an existing answer online, but I couldnt find something that would satisfy me, maybe I wasnt clear enough with the searches.

  • Currently, in the client and server scripts, I only require and initialize the modules , and the rest is handled inside of them, is this fine? Would it be better to handle stuff in the server and client script?

  • Related to the first question, the modules require a bunch of other modules, thats how i send information and call functions in between them. Is this fine? Should I instead use events? Is this performance heavy?

  • I’ve heard some people talk about “Service” modules, what exactly are those and how are they used?

  • Are there any other bad habits that I should avoid?

Here is an example for the Server Script:

local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")


local storeMod = require(ServerScriptService.Modules.Store)
local itemHandler = require(ServerScriptService.Modules.ItemHandler)

local modules = ServerScriptService.Modules

local function initializeModules()
	for i, mod in pairs(modules:GetChildren()) do
		
		if mod:IsA("ModuleScript") then
			
			local module = require(mod)

			local existingInit = module.init or module.Init
			if existingInit then
				existingInit()
			end
		end
	end
end

initializeModules()

And for example, here is an older module (to throw items away) that i made:

local ServerScriptService = game:GetService("ServerScriptService")
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")


local ItemHandler = require(ServerScriptService.Modules.ItemHandler)

local Notification =  ReplicatedStorage.Notification

local mod = {}


mod.init = function()
	for i,trash in CollectionService:GetTagged("Trash") do
		local clickDetector = trash:FindFirstChildOfClass("ClickDetector")
		
		if clickDetector then
			clickDetector.MouseClick:Connect(function(player)
				
				local item = ItemHandler.getFirstItem(player)
				
				if item then
					local s = ItemHandler.removeItem(player,item.Name)
					if s then
						Notification:FireClient(player, "Threw away " .. item.Name )
					else
						warn("Couldn't remove item:", item.Name)
					end
				else
					Notification:FireClient(player, "Nothing to throw away")
				end
			end)
		else
			warn("no click detector found for:", trash.Name)
		end
	end
end


return mod

Any issues with them?

If you need additional information, let me know and I’ll glady provide it to you.

I’ve worked in various projects where only modular OOP was allowed and can tell you there is nothing wrong with only requiring and initializing the modules, with little to no usage of server and client scripts - it is actually encouraged.

Remotes are strictly used for Client > Server communication and vice versa. If I’m not mistaken, there is no problem with sending information/calling functions with modules, just ensure its same side communication i.e you can’t call a client module on the server.

Not sure what you meant by this exactly, I’m familiar with Services and Controllers when using modules but not sure if that’s what you meant

1 Like

Thanks a lot!

About the service modules I mentioned, as an example, I’ve seen them in crusherfire’s video about his custom module loader. I know that i might have not given enough information, so dont worry if you cant really provide an answer with what I’ve listed, I dont think this matters too much anyway.

image

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