Module script table isnt sync

Hi,
I have problem that when i have table in module script and i update it with this code for example (but i think, that updating is working)

require(game.ServerScriptService.Game.Electro.Network).activity[50]=true

this code will still not print the update (and when i put there breakpoint and inspect the val, there isnt the change too)

local net = require(game.ServerScriptService.Game.Electro.Network)
while wait(100) do
	for _,v in ipairs(net) do
		print(v)
	end
end

Hey mistr,

I think you misunderstand how modules work. I would read this documentation if you want the full context explained in its entirety.

Modules are not used as a form of saving mechanism, they are used as a DRY principle. DRY is basically just the rule of “don’t-repeat-yourself”, defining behavior and allowing you to access key principles of your code. For example when I am using my auto ranking bots I use modules in order to hold the settings which I may want to call elsewhere.

When you do:

require(game.ServerScriptService.Game.Electro.Network).activity[50]=true

You essentially have changed the value for your copy of the “Electro.Network” and then because its not saved it has basically just been lost to the abyss, it doesn’t have any pointer (a constant or variable) to refer to. As such when you do the next line you have required a new version of the module.

What you may want to use instead is a simple table inside a script. Modules are used explicitly for containers that run once and returns your entire modules commands etc.

How it should be used as an example:

-- A module which can be used for creating an immunity system for players in the Immune list.
local module = {}

module.ImmunePlayers = {}

function module:CheckImmunity(name)
   for _, player in pairs(module.ImmunePlayers) do
      if player == name then return true end
   end
   return false
end

return module

Always returns the same type of value, can be applied in many ways. If you need any help feel free to reply to this. Modules are great though, can be a good skill to use when you need it.

Have fun! :smile:

1 Like

But I use modules this way somewhere else and there it works.
And also i got this info in this topic How to get value from global var? - #6 by waterrunner

Well if you do:

local Net = require(game.ServerScriptService.Game.Electro.Network)
Net.Activity[50] = true

And then operate off of that without deleting the memory pointer then you’ll be fine.

Also a global variable in a programming sense is just a variable which sits outside all functions etc. If your talking about script-to-script variables then use _G, its basically a default module.

So i can use modules to share data? (a im totally bad at understanding something)
edit: and what do you mean by

A module script will run only once - the first time require is called on it. It’s return value is cached so any require calls made afterwards will return the same value. So if a module script returns a table, and a script requires that module and modifies the table, the change will be visible to other scripts that require the same module later on.

2 Likes

so why the example isnt working, when i run it after i execute the change line in console.

If you are running a console command to change a value, then running another command after to read the value it shouldn’t work because the execution environment is different. If you try it in actual scripts and run the game it should work.

Example ModuleScript in game.Workspace:

return {}

Example Script1 in game.Workspace:

local data = require(workspace.ModuleScript)
while (true) do
    wait(1)
    print(data["test"])
end

Example Script2 in game.Workspace:

local data = require(workspace.ModuleScript)
wait(5)
data["test"] = "hello"

The output would be something like:

--  nil
--  nil
--  nil
--  nil
--           (the table is modified here)
--  hello
--  hello
--  hello...
1 Like

And is there diference if the script is in workspace or server script service?

No, whatever is returned by a module script is shared as long as you’re not crossing the client-server boundary. (i.e. if you try to use a server script to access something that was set by a local script)

1 Like