Using dictionaries as directories for ModuleScript

Hello,

My aim is to organize my library better by separating my module script into multiple different scripts which will be referenced by calling module.submodule.function(). The submodule would be a module script inside the module script. This is what it would look like in the explorer:

image

At the moment inside the main module my code looks like this to test:

local Main = {
	["Sub"] = script.Sub
}

return Main

The code in the sub module looks like this:

local Sub = {}

function Sub.Say()
print("Hello")
end

return Sub

Then I call the module from a server script using this code:

local Module = require(game:GetService("ReplicatedStorage"):WaitForChild("Main"))

Module.Sub.Say()

However the error I’m getting in the console is this:

15:34:18.937 Say is not a valid member of ModuleScript “ReplicatedStorage.Main.Sub” - Server - Script:3

Thanks for taking your time to read. Help is appreciated.

1 Like

From what you’ve shown above, it does not look like you have something called ‘Sub’ inside that module.
It looks like you have something called ‘Submodule’ and not ‘Sub’.

No it is called “sub”. I updated the topic with a screenshot of the explorer.

Sub is a module script.

You’re suppose to require Sub so it returns the table you’re trying to index instead of indexing the module script itself.

1 Like

Also be aware doing this will make it extremely difficult to browse through your module and find how a function works.

The method i use instead looks something like this:

do --Connect stuff to events
    local E = yata yata
    E.Something.YataYata.OnServerEvent:Connect(etc. etc.)
end

do --Other stuff
    --More stuff here
end

do --More stuff
    --stuff
end

And you can have do end sections inside do end sections.

1 Like