How can I make a single script architecture

Hello, I have made 5 module scripts that hold 5 prints. I wanna require all of them inside of a main module script then fire them independently from a main normal script how can I do this do I have to give all the module scripts their own variable and if so how can I do this if this is my main module code:

local modules = {}

local Main = {modules}

for i, module in pairs(script.Parent:GetDescendants()) do

if module:IsA("ModuleScript") then
	local modules = require(module)
	return modules
end
end

return Main
-- Load all services:
for _,v in ipairs(script.Parent.Services:GetDescendants()) do
    if (v:IsA("ModuleScript")) then
        require(v)
    end
end

that is what I did except the spacing may be different but what I wanna do but it won’t let me I wanna require the modules that this script is requiring how can I do this?

let me show you my workspace because I’m probably not doing a good job of explaining what I want to accomplish.

Services is just the folder the modules are parented in.

You have to insert them into a table.

local modules = {}

for _, module in ipairs(script.Parent:GetDescendants()) do
    if (module:IsA("ModuleScript") then
        modules[module.Name] = require(module)
    end
end

You also used pairs which indexes a dictionary, but GetDescendants returns a numercially-indexed table.

1 Like

the code you gave me is the excact same code I did but see im putting this code inside of a module script calling it the main module I am trying to make a single script architecture.

Sorry but I do not understand what you mean by this.

ok I see what dies ipairs do also thanks for the response let me try this

its a way of abstracting your code or simplifying your code using a normal script to require module scripts to require other module scripts this may help you if you wanna learn about it

the code is simple enough using an normal script to require another module just to require other modules is plain stupid (no offense)

this is also in a module script
using this code do I return modules because modules isnt the module name but my own table so then do return modules and put modules in the module script?

none taken theres a reason I just dont know it yet im trying to learn why people do it but I think I know its for using oop which is a really good method a lot of good scripters and devs made to transfer information between modules then call upon that information in a script instead of per say a remote event or bindable event

Heres my workspace

heres the code of one of my child modules

local class1 = {"Hello"}

return class1

heres the code of my main module

local modules = {}

local main = {modules}

for _, module in ipairs(script.Parent:GetDescendants()) do
if module:IsA("ModuleScript") then
	modules[module.Name] = require(module)
	return modules
end
end

return main

and heres my main script code

local main = require(script.Parent.mainModuleScript)

print(main.modules[1])

I never really heard of people using other modules to require other modules in your use case honestly. Unless you are creating an module handler script to handle your modules and what and who can use them. If you are trying to learn oop i recommend this post Concepts: Object Oriented Programming in Roblox

Its just a method of transfering information its supposed to be very useful but still I do not know how to make them also I titled this post wrong

this is the way i do it

local ss = game:GetService("ServerStorage")
local rs = game:GetService("ReplicatedStorage")
local client = rs:WaitForChild("client")
local server = rs:WaitForChild("server")
local a = {}
function FindFirstChildNCS(obj,name)
	for i,v in pairs(obj:GetChildren()) do
		if v.Name:lower() == name:lower() then
			return v
		end
	end
	return
end
function WaitForChildNCS(obj,name,timeout)
	local z = os.time() + timeout
	repeat wait()
		for i,v in pairs(obj:GetChildren()) do
			if v.Name:lower() == name:lower() then
				return v
			end
		end
	until os.time() >= z
	return
end
function a:GetServices()
	local y = {}
	for i,v in pairs(ss:GetChildren()) do
		if v:IsA("ModuleScript") then
			y[v.Name] = require(v)
		end
	end
	return y
end
function a:GetService(name)
	local y = WaitForChildNCS(ss,name,5)
	print(y)
	return (y == nil and assert("Service could not be found.") or require(y))
end
function a:GetClientEvents()
	local y = {}
	for i,v in pairs(client:GetChildren()) do
		if v:IsA("RemoteEvent") or v:IsA("RemoteFunction") then
			y[v.Name] = v
		end
	end
	return y
end
function a:GetServerEvents()
	local y = {}
	for i,v in pairs(server:GetChildren()) do
		if  v:IsA("RemoteEvent") or v:IsA("RemoteFunction") then
			y[v.Name] = v
		end
	end
	return y
end
return a

this isnt excactly what im trying to do but maybe Im just explaining it wrong or attempting something impossible

Is this what you mean?

local Module = {}

local Module.Modules = {};

for _, Object in next, script.Parent:GetDescendants() do
	if Object:IsA("ModuleScript") then
		Module.Modules[Object.Name] = require(Object);
	end
end

return Module

------------------------------------------------------------

require(Module).Modules["CoolRequire"].CoolFunction()

Since your using return inside your loop once this module is required it would only return that ModuleScript.

Also you’ll need to set modules inside the main table to something instead of just making it error.

local Main = {}
Main.modules = {}

for i, module in pairs(script.Parent:GetDescendants()) do
    if module:IsA("ModuleScript") then
        table.insert(Main.modules, require(module))
    end
end

return Main
local main = require(script.Parent.mainModuleScript)
local data = main.modules[1]
print(data)