Why aren't required modules' functions replicating in a module?

I have a module that is using a bunch of other modules to organize code. My issue is that the code from the other modules isn’t showing up when I require the main module!

Please help!

What does the require autofill look like? If it’s not autofilling but the methods exist, it will still work

Just a similar example I wrote

Main Module: (workspace.Module1)

local module = {}

local functions = require(workspace.Module2)

return module

Required Module (workspace.Module2)

local module = {}

function module.Test()
	return "Tested!"
end

return module

Example Script:

local module = require(workspace.Module1)

module. --Function doesn't show up from required module!

Module1 doesn’t return anything from Module2 to the example script

That’s my exact issue, I tried changing

return module

to

return module, functions

inside of Module1

return functions

that works for this case, but what if I changed Module1 to this: (like in my actual module)

local module = {}

local functions = require(workspace.Module2)

function module.Test2()
   print("Test 2!")
end

return functions

that would only return Module2’s functions, but not Module1’s

In this case, you’d want to use metatables:

return setmetatable(module, {
    __index = functions
}) 

This will return the module table to the script and attach an __index metamethod to it. When you try to access a function or property that doesn’t exist inside module, it will search inside the functions table for it

I recommend reading this for an understanding of metatables:
(Metatables | Roblox Creator Documentation)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.