Creating a module to gain access to all other modules?

So one of my friends made an encyclopedia module, that requires everything so it loads, and Utilities. But you can’t require it from the module if you need it. Is there a way I can load all of the modules then return them to easily get? I was thinking maybe loop through and require, and maybe insert into table? would that work?

I tried this just a basic test:
script:

local test = require(game:GetService("ServerScriptService"):WaitForChild("test"))

wait(5)

for i, v in pairs(test.Modules) do
	print(i, v)
end

test.Modules[1].test()

Module:


local test = {}

test.Modules = {}

for i, v in pairs(game:GetService("ServerScriptService"):GetDescendants()) do
	if v:IsA("ModuleScript") then
		table.insert(test.Modules, v)
	end
end

return test

He never seemed to require the module that is inside of the table.

local test = require(game:GetService("ServerScriptService"):WaitForChild("test"))

wait(5)

for i, v in pairs(test.Modules) do
	print(i, v)
end

require(test.Modules[1]).test()

That wasn’t his script, just a messy one I made quickly. I didn’t notice that, can I return all the required things? Can I return them required or do I have to do it this way and will this even work? Yet alone be efficient

then you can simply insert the required stuff instead of the module itself.

local test = {}

test.Modules = {}

for i, v in pairs(game:GetService("ServerScriptService"):GetDescendants()) do
	if v:IsA("ModuleScript") then
		table.insert(test.Modules, require(v))
	end
end

return test

And how can I get a specific one any way to just do test.ProfileService or something?

oh, you could set the name of the table to the name of the module.

local test = {}

test.Modules = {}

for i, v in pairs(game:GetService("ServerScriptService"):GetDescendants()) do
	if v:IsA("ModuleScript") then
		test.Modules[v.Name] = require(v)
	end
end

return test
2 Likes

One question, say I call this module that has the modules it in from another script again will it require everything again and will that cause issues?

Yes, it will require all of the modules again, and no, it should not cause any problems.

Doesn’t that mean anything I do reruns? Even if I planned on the module to go once. + When i did it didn’t seem to work. Should I maybe set the table to a global variable