Require All Module Script Inside A Folder

Is there a way to require all modules inside a folder,
So is like I just need to make like module:GetModules(FolderName)
and in server script I just module:GetModules(“PlayerModules”) and Inside this PlayerModules folder there are several Modules
So I just
local PlayerModules = module:GetModules(“PlayerModules”)
local One = PlayerModules.One
One:Test()

You can always just require all your modules in a dictionary and fire their functions through it

You could try something like this. I haven’t tried it, so I’m not sure if it would work.

local module = {}
for _, x in pairs(FolderName:GetChildren()) do
	module[x] = require(script.Parent.x)
end

Obviously, replace script.Parent with whatever matches your environment. So if you have a module called Foo with a method called Jerry(), you would use it like this:

module.Foo.Jerry()

Hope this helps.

Well I don’t want I just want to like take the folders and require all inside it.

local ModuleScripts = {};

for Index, Module in next, ModuleFolder:GetChildren() do
ModuleScripts[Index] = require(Module);
end;

Simply do what @Maelstorm_1973 said

local moduleDir = -- the path of the module
local moduleStorage = {}
for _, modules in pairs(moduleDir:GetChildren()) do
	moduleStorage[modules.Name] = require(modules)
end

Then use them as you’d like. Let’s say I have a module calls Raycaster:

pcall(function() -- ensure the module is required
    moduleStorage["Raycaster"]:FireCast()
end)

Unless, you really mean you want to require the modules in the client itself, then in the server you just do player:GetModule() and take it from that client then it surely won’t work because it’s only included in that client

My latest module script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local RunService = game:GetService("RunService")
local PlayerServicesClient = ReplicatedStorage.Services
local PlayerServicesServer = ServerStorage.Services
local IsClient = RunService:IsClient()
local IsServer = RunService:IsServer()

local customService = {};

function customService:GetService(requestedService)
	if IsClient then
		local targetedFolder = PlayerServicesClient:FindFirstChild(requestedService)
		local moduleStorage = {}
		for _, modules in pairs(targetedFolder:GetChildren()) do
			moduleStorage[modules.Name] = require(modules)
		end
	elseif IsServer then
		local targetedFolder = PlayerServicesServer:FindFirstChild(requestedService)
		local moduleStorage = {}
		for _, modules in pairs(targetedFolder:GetChildren()) do
			moduleStorage[modules.Name] = require(modules)
		end
	end
end;

return customService;

So later in the server script or local script, I just,

-- Already required the Module
local PlayerData = customService:GetService("PlayerData") -- The Folder
local Data = PlayerData.Data -- this is the module inside PlayerData Folder
Data:Test()

Do you think this is gonna work?

Ah it doesn’t work, do you know how to fix this?

This is like the folder trees?
Thisszz
So I want let’s say like using the module customService:GetModule(serviceName)
then in the server script

local playerData = customService:GetModule("PlayerData")
local Modulo = playerData.Modulo
Modulo:Test()

Well it’s like GetService, you know when you already GetService something you just need .name or [name]

Unfortunately, I don’t think there’s a clean way to do that in Roblox’s version of LUA, or any version of LUA. Even in languages like C/C++, Java, PHP, Perl, etc… you still have to include each header file/library separately and then possibly use additional flags to the linker to include other libraries for those languages that are not interpreted. Even the current favorite, Python, you have to manually include things.

For what you want, you are going to have to execute a loop of some sort on the children of the folder. And the way it works, you are going to have to place that in a variable. If you do it one at a time without a loop, you can specify the different names. Inside a loop, that is not really possible…

With that being said, you might want to do what is known as a meta file. You include the one file from the folder, but that file includes all the other files in that folder. If it changes, you would have to modify that file to reflect those changes. Something like this:

moduleFolderMeta = {}

includeFile1 = require(script.Parent.File1)
includeFile2 = require(script.Parent.File2)
includeFile3 = require(script.Parent.File3)
includeFile4 = require(script.Parent.File4)

return moduleFolderMeta

Note that I DID NOT specify the local qualifier for each of those variables. LUA may complain about it, but they would be considered global. That will probably be about that best that you are going to get with the limitations of the LUA language.

1 Like

I have an idea that i try it works but i dont know about performance, do you know how to check script performance to the server.

-- script 1
for i, descendant in ipairs(game.ReplicatedStorage:GetDescendants())
    if descendant.ClassName ~= "ModuleScript" then continue end
    _G[descendant.Name] = require(descendant)
end
-- script 2
_G.ModuleScriptName.MyFunction()

this video might help

1 Like

I know that F9 brings up the developer tools panel. There’s some profiling tools in there.

I built something like that forever ago. It needed a few specific features though. I never got around to cleaning up the code, but it should work (hopefully)

ServiceHandler.rbxm (2.2 KB)

By default it will add all descendants of it’s parent as modules you can require. It will just store their name and location though. It won’t actually require them until you ask. If you need it to require the service immediatly you can add a ‘required’ = true attribute to the module and the code will preload it.

If you want to change the root of descendants it’s looking for, just change where SOURCE (top line) points in the script.

Now for the two functions for you.

This first one will just directly return the service by name
module.GetService(moduleName)

This one will start requiring the service, but will not yield your code.
module.PromiseService(moduleName, callbackFunction)

Keep in mind that it needs to be called using . right now. You can probably change that to : super easily though.

Also if a module is a descendant of another module, it won’t actually add it to the list. This is because it assumes if you are doing that, then that lower module is not meant to be required by anything else.

I don’t think this would be very useful. I mean, you still need to explicitly name each module to use it, so what’s the point? All this would do is hide your dependencies

1 Like

It’s only useful in the sense you don’t have to spell out the specific paths every time which can be annoying. But other than that there is very little point to doing this. I never really used my module for this. But with a certain design structure for your scripts this could be useful like if you have a ton of modules that have a specific function with no exterior dependencies you can organize them in the folders however you like while retaining simplicity in loading it. But yes. Very small practical use case as you can load it the normal way anyways.

I had intended to use it at one point just to load in some modules that I add to every project of mine just for the sake of simplicity. I just never got around to implementing it in an actual project though.