and then inside the data module, require the module that is calling it, the scripts break.
What’s basically happening, is I’m trying to be able to access the players data table from any module. So for any module to access and change data, they need to require the DataModule. But if the DataModule needs to require them as well, they become broken.
A better example being say this:
function dataManager:SaveData(player)
if not plots:FindFirstChild(player.Name) then return end
savePlot:Save(player) -- A saving module
playerDataStore:SetAsync(player.UserId, self.PlayerData[player.UserId])
clearPlot:Clear(player)
end
But then the saving module needs to require the data module again to save stuff:
local user = dataManager.PlayerData[player.UserId] -- required earlier on the script
if not user then return end
user.Items = playerItems
Require both modules in a script, pass the second module to the first one via a function like “Setup” and vise-versa. Or call module #1’s setup function from module #2 passing along module #2. Don’t require them in the stack, keep the variables set to nil til after the setup functions have been called. Once that happened you can use both modules. That’s how I do it for my framework.
Idk if I explained it well enough, if not I’ll provide examples.
For my modules i usually have a function called init (initialization), and it doesnt conflict that way.
example
-- module 1
local module2 = false
local module = {}
function module.init()
module2 = require(module2.location)
end
return module
-- module 2
local module1 = false
local module = {}
function module.init()
module1 = require(module1.location)
end
return module
-- The main script
local module1 = require(module1.location)
local module2 = require(module2.location)
module1.init()
module2.init()
Yeah, that’s pretty similar to what I replied with. Only thing is, you’re creating two instances of each module instead of passing on the existing instances from the script you called “init” in.
For your information, this method is called ‘lazy loading’ and in some cases it is faster to use modules this method, generally when you want to run the module at a later point, rather than immediatley (as you are meant to be removing all yielding)