Are two modules able to communicate between each other?


This is more of a question about modules & variables, rather than needing a script fixed but it regards scripting; hence why this is here.


Explanation

I know this sounds a lot more confusing in the title but let me explain in detail what I mean;
Are two modules able to communicate between each other?
I ask because when I try to create a variable to a module that’s attempting to communicate back; I get this issue;
image
I already have a set of variables in my Functions module calling to my Settings and Commands module, I believe this is what causes it; however I may be wrong.
image


Thank you for any help you can give.


After Answer

It is possible and is very easy to make, you use a separate module to communicate between each module and return that module being required.

I don’t think you can do that but I’m sure you can communicate with one module with another but not from the module you are communicating it from.

Could you please elaborate on that? I’m still pretty confused as to if you can or can’t. You said that you think you can’t do it then said you can communicate with one module with another but not from the module you’re communicating it from. That doesn’t make sense to me at all.

Let’s say we have module1 and module2. module1 is having a index called “a” module2 is calling module1 by require(game.ServerScriptService.module1).a(). now with this module2 can add more things in module1 by adding others module into and hence when you are done, you can out module2 which in total calls all the modules in one time. (Note that this can also be done with more than 2 modules!)

1 Like

Yeah, I’m still quite confused… this makes no sense to me. Could you give an example of this?

I’m not entirely sure what you are attempting to do with this but you don’t want to get a module through a module and I’m almost certain you can’t but if you can it is still bad practice.

You can send variables through server/local scripts to the module scripts. If you want the module to do the math with the variables you need to send them through the server/local script.

You can simply send the variables you need by requiring a different module from the source code and then use that in your math module.

You can’t as others have said already. It would either cause your game to crash or the script to timeout because it’s essentially requiring module1, and on requiring module1, module1 requires module2, then upon module2 being required, module2 requires module 1, and so on, infinitely fast (though, I’m not sure if this would happen if the table you’re returning requires the other module but as @Lametta said, it’s bad practice)

I suggest you implement a sort of “middleman” or a third module which handles communication between the two modules.

For example here’s some pseudocode to give you an idea,
module1:

local communicationModule = require(script.Parent.CommunicationModule)
local module = {}
-- let's say for whatever reason we want to change a value inside of the communication module
communicationModule:UpdateValueForModule1(10)
communicationModule.Module2ValueChanged:Connect(function()
    print('Module2\'s value changed!')
end
return module

module2:

local communicationModule = require(script.Parent.CommunicationModule)
local module = {}
communicationModule:UpdateValueForModule2(10)
communicationModule.Module1ValueChanged:Connect(function()
    print('Module1\'s value changed!')
end
return module

CommunicationModule:

local module = {
    ['Module1Value'] = 5;
    ['Module2Value'] = 5;
    ['__internalModule1ChangedEvent'] = Instance.new('BindableEvent')
    ['__internalModule2ChangedEvent'] = Instance.new('BindableEvent')
    ['UpdateValueForModule1'] = function(self, value)
        self.Module1Value = value
        self.__internalModule1ChangedEvent:Fire()
    end;
    ['UpdateValueForModule1'] = function(self, value)
        self.Module2Value = value
        self.__internalModule2ChangedEvent:Fire()
    end;
    ['UpdateValueForModule1'] = function(self, value)
        self.Module1Value = value
        self.__internalModule1ChangedEvent:Fire()
    end;
}
module.Module1ValueChanged = __internalModule1ChangedEvent.Event
module.Module2ValueChanged = __internalModule2ChangedEvent.Event
return module

Another benefit of this is that you can listen to the one value to change from multiple other scripts.

3 Likes

In theory, this would work but you would run into cyclic require errors.

1 Like