Hey there, I just wanted to know the best way to organize my Modules and RemoteEvents, etc. I have them both in designated folders, one for all of the modules, and one for all RemoteEvents, and they’re all folders in ReplicatedStorage. Is this the right choice? If not, then what’s better?
I’d recommend you to be more specific with your folder. Like Car modules, player modules same for events / functions. Example:
And also have a module loader so you dont have to require things a million times.
Interesting, could you please explain how this module loader works? Or is it simpler than I think it is?
ServerScriptService
- Script (Module Loader for Server) (require module and then you can initialize it)
ServerStorage
- Folder to store all Server Modules
ReplicatedStorage
- Shared Modules or Events
StarterPlayerScripts
-
Folder for client modules
-
Local Script will be located here too.
The locating for Server Modules is honestly preference. There might be some difference, I am not 100% sure.
function moduleLoader:load(moduleName)
if moduleName == nil then moduleName = '' end -- make sure the thing thats requiring the module isnt also a module (else it would loop)
local modulesFolder = script.Parent.Modules:GetDescendants()
local modules = {}
for i, v in pairs(modulesFolder) do -- loop through module folder
if v:IsA("ModuleScript") then -- if its a module script then
local cModule
local scs, err = pcall(function() -- pcall so when required module error it dosent break the entire script
if moduleName ~= v.Name then
cModule = require(v) -- replace cModule with the required module
end
end)
if scs and moduleName ~= v.Name then -- check if success and the thing thats requring the module isnt itself (might dosent make sense but just.. yeah)
modules[v.Name] = cModule
elseif err then -- if error then print this
print("MODULE REQUIRED GOT AN ERROR: "..tostring(v))
end
end
end
return modules
end
Just do a loop through your modules folder and require them from there.
Thats how I made my module loader.
Thanks for this, I was having a hard time understanding where would be the best area to place different modules.