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.