Im saving the data at a script rn and i wanna update it from other scripts so i came up with using module scripts as a SaveModule so i can acess the module from wherever i can and whenever i can via filling arguments but then i thought is this optimized and a good idea…
modules are used mainly to store and organise stuff. If not for some people idk why, but i think datastore is both client and server if im correct idk.
overall it can be a good idea. i think.
- Good Idea
- Bad Idea
0 voters
module scripts are not bad for performance but note that the client cannon access modifications that happened to a module from the server and vice versa because each one run in a different environment
You can save data to a ModuleScript
but its only temporary. And does not replicate to client (if changed by server) and vice versa. (Singleton in each environments)
ModuleScript
--- modulescript
local m = {}
m.foo = 2
return m
Script/LocalScript
local module = require(modulescript)
module.bar = "im bar!"
--- you can do this in another LuaSourceContainer but it has to be in the same RunContext
local moduleagain = require(modulescript)
print(moduleagain.bar) -- outputs "i am bar!"
It’s completely fine and good for performance - likely better than a lot of value objects or that sort of thing, and it means you can centralise your game’s data (client and server remain separate though). This can make things a lot cleaner and easier to work with in a few cases.
It has benefits and downsides. One benefit being centralisation, but a downside is the lack of automatic replication, which you get with Value objects and other instances.
For a while I stuck only with using modules for data, a bit too much. Don’t limit yourself just to modules though! You could even use a mix in cases where it’s significantly easier to just use a value object for updating something small on the client, rather than updating the server’s module value, and then firing a RemoteEvent to the player, and updating the client value, and then updating the actual result from that.
Keep it simple and easy and fun to work with. My exploration into ModuleScripts for data storage ended up with me being too strict with it and making my systems harder to work with, not easier - but you don’t have to do it that way.