How to share module tables

Hey, I was just curious about how to share module tables between different scripts because I’m scripting a module for my other module to share things between different libraries

2 Likes

You can require a single module from as many scripts as you want.

Yeah but the module tables won’t be the same in all scripts

Can you provide a bit more insight? Your problem is not vivid.

For example: I’ll require the module from script1 and while using script1, the modulescript writes a new table declared as local, then script2 tries to get that table but it’s nil

Something like this script

local module = {}
local objectsCreated = {}

function module:createnewobject()
   table.insert(objectsCreated, math.random(1, 10))
end

return module

The objectsCreated table won’t be the same for all scripts

Are you trying to exchange tables between scripts? For that, I recommend using Bindable Events.

My module have like “data” that an script can create, but the main problem is that the others scripts can use that “data” too, but I do not know if the module is global or local(attached to the script that required it)

Have you tested that? Because I used a global ‘Variables’ ModuleScript in one of my projects, and that transferred between scripts when required(). Though it won’t be shared between server and client, even if it’s in ReplicatedStorage.
I’m not sure how it functions with OOP, as I didn’t originally test it with that but I don’t see why it’d be any different. Test to make sure, but it should work.

1 Like

Yeah, I think if I define the tables inside the table that module returns may work.

1 Like

Yep. Also changing any values inside the tables using other scripts WILL be shared, not just the original value. That might’ve been the original question but just to make sure.

You might want to use one of the global modules that roblox provides
Suggest something potentially like this

shared["Data"] = require(module)
_G["Data"] = require(module)

You can use this, but it’s better practice to use ModuleScripts. I can’t remember the exact reasons but there are some issues with built-in global variables that make ModuleScripts a better alternative.
Searching on the DevForums should find you some answers if you’re interested.

2 Likes

Please don’t do this; _G / shared are deprecated and ModuleScripts should be used instead. They’re really limited in their use and they are code smell for some very worrying engineering decisions that’ll make your life really difficult down the line trying to track down where (and when!) things are defined.

@midnightsnacks_fan - The ModuleScript is only initiated once on each client / server; so, any future references to the ModuleScript reference the environment created with the original require.

Example:

-- ModuleScript.lua

local Module = {}
local Value = 0

function Module:AddOne()
  Value += 1
end

function Module:GetValue()
  return Value
end

return Module
-- ScriptA.lua (loaded first)

local Module = require(script.Parent:WaitForChild("ModuleScript"))

Module:AddOne()
print(("[ScriptA] %d"):format(Module:GetValue())) -- 1
wait(2)
Module:AddOne()
print(("[ScriptA] %d"):format(Module:GetValue())) -- 3
-- ScriptB.lua (loaded second)

local Module = require(script.Parent:WaitForChild("ModuleScript"))
wait(0.5)
print(("[ScriptB] %d"):format(Module:GetValue())) -- 1
wait(1)
Module:AddOne()
print(("[ScriptB] %d"):format(Module:GetValue())) -- 2

Try it - it’ll work!

The same works for the table itself; this is just a better illustration. If you want a “clean” environment, you should use an OOP “new” constructor, which clones the table via the __index metamethod.

6 Likes