Starting Modules

Hey Developers, I’ve been working on a UI system and most of it is Module scripts. I’m having trouble actually getting them to run tho.

image

Here’s how it’s set up: script lib contains all of the tweenings, the intro is the module script that contains the intro to run and core is the main UI. But every time I run the game it doesn’t load the module. I also want to have this placed in Playerstarterscripts if that is possible.

1 Like

You can have this is PlayerStarterScripts. In your example, to use the module scripts it looks like Hub would need to require(this.Intro) and require(this.Core). Depending on what is in your module scripts, Hub would probably need to call Intro.ShowGui() etc. for it to actually do something.

You need to use the require(module) to work so for example require(script.Intro) I believe

You can use require(module) to require it, for example
MODULE SCRIPT:

local module = {}

function module.Run()

end

return module

SCRIPT

local ms = require(script.Module)

ms.Run()

You can also hold tables like this

return {
   [1] = script.Parent,
   [2] = script.Parent.Parent
}

Where would the tables be held?

If you don’t want to require every single module, you can try and use this script:

local modules = {}

for _, module in ipairs(script:GetChildren()) do
	modules[module.Name] = require(module)
end

return modules

This will return a table with all the children modules. Use it in this setup:
Screenshot 2021-08-05 115014
The code should be placed in UITools and Libraries or any other module that contains other modules inside of it.

Basically, it’s a replacement for a folder. You can then do:

local uiTools = require(thePath.UITools)
-- And use all modules that are inside instead of requiring each one
uiTools.Core.MyCoolFunction()
uiTools.Libraries.MyAwesomeTweenHelper()

Also make sure that your modules return all of the values inside them, like the functions. Eg:

-- The Core module
local core = {}

function core.MyCoolFunction()
    -- Your code
end

return core -- If you don't do this, it's empty as far as other scripts know

Hope that helps!

Would

local modules = {}

for _, module in ipairs(script:GetChildren()) do
	modules[module.Name] = require(module)
end

return modules

go into the local script?

here’s how the Intro is setup: image

Right inside the table

local module = {
-- this is the table
}
return module