Storing Global Cashe with a ModuleScript: is there an Optimal Solution?

Здравствуйте! I got a question regarding Module Scripts:

How do I store ‘Global’ cash most effiecently?

By ‘Global’ Cashe I mean Cashe that for all requirees would be the same.

My Module is a Server Module in that it works on the Server and only accepts Server requirees.
The Data that I need to save are metatables and I found some ways that may do it, but I haven’t succeeded.

My Attempts:

I decided to have BindableEvents that let the Module communicate with a ServerScript. The Event would send the Metatable data and it would be added to the array from the ServerScript whom catch the event.

I also made 2 more BindableEvents, 1 for signalling the ServerScript to send the Cashe to the Module, and other for sending the Cashe to the Moduke.

My Challenges:

For starter, I do not know how to effiecently continuesly update the cashe in all the requirees of the Module without it being out of sync. I also do not know if the BindableEvents would get overqued and start to drop events, which would not be good for syncing.

You might ask: ‘Why just save the metatables inside an array in the script that needs the module?’, But since I am working on a public module, that would be annoying for the users and not solve the Issue of other scripts within the Module’s childpath needing it.

To make it clear: All I need is a good technique for storing Global Cashe on a Server ModuleScript.

you could write a setvalue function or even use metamethods to replicate changes to a client using __newindex

Like I said, I do not communicate at all with the Client with my ModuleScript.

ok so what is the thing you want to do? have a global cache?

Modulescripts store references to a table that is returned when you require

if you edit the modulescript values from one script it will also edit them in all the other scripts.

What do you mean by ‘ModuleScript values’? The Array of Metatables I want to store?

Yes, and that is Local Cashe. The Cashe is the same for the requiree (the script that required), but other requirees have their own personal Cashe.

And if you mean by having an Array built in the Module Script with the array of metatables? That would not work for me as I need to store the Metatables that are created via the functions in the ModuleScript.

Why can’t you store an array of metatables in the modulescript?

no they dont lol, a modulescript only runs on the first require and just returns the reference of the modulescript table

task.wait(0.1)
local Mod = require(Modulescript)
--since this modulescript already was required it just returns the table
--It is a reference not a copy so we can set a key value pair
Mod.Value = 1
local Mod = require(Modulescript)
print(Mod.Value)--prints nil because Value hasnt been set yet
task.wait(0.5)
--^^waiting for the other script to run
print(Mod.Value)--prints one because Value has been set in the other script

Every subsequent require of a ModuleScript on the same machine (server, client and the command bar) will always return the same instance. If you are using a ModuleScript to cache data it will return the exact same instance every time.

If you need to pass data around through events (but not through modules), you will need a pure Lua signal class to use. Both bindables and remotes do not transfer metatables when tables are passed as arguments to them so a pure Lua class will allow you to properly transfer the full table over.

You don’t need to do anything special to create a global cache so long as all scripts of the same machine are requiring that one ModuleScript. Just note this gotcha about Roblox events that they don’t send metatables with tables.

1 Like

If so then why when I have my array that has the metatables in it return empty when I send this array via a function to the requiree.

Maybe the metatables of the modulescript array don’t replicate?

are you sure your using the getmetatable method instead of just printing the table?

eg:

print(getmetatable(RequiredModule))

The Module itself is just a metatable with the functions for easier use, not the cashe.

The cashe is stored in an array in the Module script where it carries the metatables of the data generated by functions in the functions of the Module.

And all I need is the raw array of metatables that I send to the requiree.

The cashe should be stored in the modulescript, you might have it as a local variable

local modulescript = {}
modulescript.Cashe = {}

return modulescript

(sorry for the edit, i accedently sent too early)

function Module.Update()
 assert(Data ~= nil,"Function environment not updated correctly.")
 globalStorage = Data
 return Data
end

-- any script
local Module = require(Module)
local _env = getfenv(Module.UpdateTable)
_env.Data = NewTable
Module.UpdateTable()