Help with module script

So I have a module script which contains important information. I want the changes to the module to be global, in other words, when I modify the module’s content, the other scripts will also receive effect.

Here is an example of what I mean:

Module script

local module = {}
module.Word = "original"

return module

Script 1

local mod = require(game.ReplicatedStorage.ModuleScript)
mod.Word = "new word"
print(mod.Word) --> new word

Script 2

local mod = require(game.ReplicatedStorage.ModuleScript)
print(mod.Word) --> original

How can I make it so that script 2 can see the changes that script 1 did to the module?

1 Like

Try creating a function in the module to do it.

function module:ChangeInfo(newInfo)
    self.Word = newInfo
end

It worked. But how can I make it replicated cross server-client? The change it made on the server, I want the client to receive the changes too.

1 Like

You’d need to run the function on the client as well. When the server makes the change, you can fire a RemoteEvent to the client to change is as well. Fire the info along with it.

--server
RemoteEvent:FireAllClients(newInfo)
--client
local function update(newInfo)
    yourModule:ChangeInfo(newInfo)
end

RemoteEvent.OnClientEvent:Connect(update)

Thanks, that worked. I wish I could set both posts as solution. Do you mind reposting it again in one post so I can mark it as the solution?

well it seems a bit pointless… just pick one of them. The first one was probably more helpful than the second because it told you how to edit it in the module. Glad I could help though.

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