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!
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
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.
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
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 Network = {}
--- variables --
function Network.doSomething()
local Library = require(game:GetService("ReplicatedStorage"):WaitForChild("Library"))
print(Library.Custom)
end
return Network
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