How do i require the library module while being a module that is in the library module without it recursing

Hello! I wanted to pass a Requested module was require recursively error because I’m trying to make a Library module thingy and I currently have no idea on how to this.
If you can help that’ll be great!

this is the Library Module

local Library = {}

task.spawn(function()
	Library.Network = require(script["1 | Network"])
	Library.Custom = require(script["2 | Custom"])
	Library.Services = require(script["3 | Services"])
end)

return Library

This is the part of the Network module that is making that error

local Network = {}

--- variables --
local Library = require(game:GetService("ReplicatedStorage"):WaitForChild("Library"))
2 Likes

By not making it recurse, the reason they have this error in the first place is because recursing modules can cause potential performance issues. The thing that is causing the recurse is because you’re requiring the library module in the network module but also the network module in the library module which is seen as a no no by roblox.

The best way to get past it is by finding a different method, which is kind of up to you, some people use bindable events, or by passing the module through a function kind of like this

Unsure if this works since I’ve never actually tried it before but I have had this recurse problem and the way I fixed it was just not having any reason I needed to call each other.

-- LibraryModule
function libraryFunction(networkModule)
    -- Your code here
    networkModule.networkFunction()
end

-- NetworkModule
local LibraryModule = require(game.ServerScriptService.LibraryModule)

function networkFunction()
    LibraryModule.libraryFunction(self)
    -- Your code here
end

If i was to pick the function choice wouldn’t it still error since I’m still requiring the library module

Well, I tested it… something went terribly wrong and I found out why you never recurse modules

On the bright side, this failure that i’ll blame on a lack of sleep, just made me think of a way, instead of sending the module itself just send the thing you need from the module over there, for example,

-- LibraryModule
LibraryModule.libraryFunction(networkTable)
    -- Your code here
    print(networkTable)
end

-- NetworkModule
local LibraryModule = require(game.ServerScriptService.LibraryModule)

NetworkModule.Tableofstuff = {
WOW = 0,
NO = 2
}
function networkFunction()
    LibraryModule.libraryFunction(NetworkModule.Tableofstuff )
    -- Your code here
end

This is just a basic concept you may be able to do this with returns, I believe, but there’s probably a better way of doing it that I just can’t think of rn. You can also send over functions (through a table not sure if the tables needed though)

So I guess there’s no other way? I wanted to access other modules inside the library within another module inside the library to get it’s features but I don’t think there’s any other way.

Had the same issue so I got an idea:

Main Module:

local module = {
yourOtherModule= {}
}

require(yourOtherModule)(module)

--now also here usable
print(module.yourOtherModule:BlaBla())
return module

yourOtherModule:

return function(module) -- you can access module now
--if you want to add functions to module and make it everywhere usable do
  function module.yourOtherModule:BlaBla()
        return "yo"
  end
end

just require those modules instead of the library module. thats how everybody does it

Won’t work? Because they call them booth

Not something I would recommend too much because it can get confusing,

but a way around the issue is requiring the library module within a function of the inner module. This way the recursion does not happen on require(), but rather the function call. That keeps the endless loop of requiring from happening.

Or you could pass the library as a parameter to the functions. Either way should work.

– edited library module script to not use task.spawn from your example

local Library = {}
Library.Network = require(script["1 | Network"])
Library.Custom = require(script["2 | Custom"])
Library.Services = require(script["3 | Services"])

return Library
local Network = {}

--- variables --
function Network.doSomething()
   local Library = require(game:GetService("ReplicatedStorage"):WaitForChild("Library"))
   print(Library.Custom) 
end

return Network

Library.lua

Local Library = {}

task.spawn(function()
	Library.Network = require(script["1 | Network"])
	Library.Custom = require(script["2 | Custom"])
	Library.Services = require(script["3 | Services"])
end)

return Library

Network.lua (example)

local Network = {}

local Services = require(script.Parent["3 | Services"])

if everything requires everything else (idk why you would have this), you will always get a require loop unless you do some annoying stuff with global variables

Using my method won’t cause this problem and its very useful as an example you create a main module for an anticheat it has variables and functions which you also want to use on a sub module from the anticheat thats why the submodule needs access to the main module and the main module access to the functions from the sub module

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