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
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?
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.
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.
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
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
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
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
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)