Issue with storing and transferring data using modulescript variables without using instances

Don’t really know how to exactly describe the issue which is why I’m gonna share the scripts so you can replicate it:

modulescript in serverscriptservice:

local module = {}
module.Data = {}
function module.SeeData()
    print(module.Data)    
end
function module.FillData()
    for i = 1,10 do
      module.Data["hello"..tostring(math.random(1,999999))] = true
    end
end
return module

and this into a normal script and the parented to the module script (prints everything normally)

local mod = require(script.Parent)
mod.FillData()
while true do
    task.wait(2)
    print(mod.Data) -- this works (Prints correct data)
    mod.SeeData() -- this works aswell (Prints correct data)
end
    --[[doing this exact same thing in another script (without mod.FillData() )
    or the command bar doesn't print anything and shows the table as empty]]

Second script parent it to the module script aswell (prints everything normally)

local mod = require(script.Parent)
while true do
    task.wait(2)
    print(mod.Data) -- prints like in the other script
    mod.SeeData() -- prints like in the other script
end

but when you put the equivalent code to print the array table in the command bar it prints {} (empty table)

--where Unexpected output happens prints empty table
require(game:GetService("ServerScriptService").ModuleScript).SeeData()

--prints {} (empty table)
print(require(game:GetService("ServerScriptService").ModuleScript).Data)

Basically this’ll replicate the issue I’m having/ unexpected output
I’m aware that when a script requires something it gets a new copy of the module script (explanation I got once not sure if correct) and doesn’t share the variables anymore, so my question is how do I fix this issue and be able to do this with only module scripts and no instances (if this isn’t possible tell me but i think it is)

I think if you change things up and generate data inside of the same modulescript then the scripts will be printing {} aswell if anyone can explain these anomalies to me it’d be greatly appreciated since I’m trying to expand my knowledge on Luau

When a ModuleScript is required from multiple scripts, it does not create a new copy of the ModuleScript. The closest thing in creating a new set of variables, is if you have explicitly created some sort of class/object. But even then, the objects created would still be contained within the ModuleScript it self, and that data would be transferable to other scripts.

Just make sure you only use “server-sided” ModuleScripts only on the server, and vice-versa on the client. If you require both a ModuleScript on both the server and client it may result in unexpected data being streamed due to cross network replication.

Edits: Made some changes to make the explanation more coherent.

It shows nothing when you don’t call the FillData function because it doesn’t fill the table. It’ll be an empty table until you fill it, hence why it would output ({}), nil, or straight-up nothing.

Yes so in my example to replicate my issue I conform to have everything Server sided, there is no network replication being done in my example since you see me mentioning in the beginning to parent the modulescript to serverscriptservice and then use the command bar (from Serverside obviously) aswell

I’m actually running FillData() at the start of me running the example, and then after I’ve ran FillData(), the issue is that it shows the table being full in one script and then in another script (the commandbar) it shows it as empty (I think there is another scenario if I put FillData() somewhere in a function in the modulescript itself where the data is stored It’ll show empty in all scripts for some reason aswell)

Actually after running your code, the code examples you sent above do work. The data should be streamed correctly due to the mod.FillData() function you called in one of your server scripts.

Just so you know, Dictionary's do not have a length, this is because they do not have numerical indices. This is why you may be getting an “empty table”. If you want to get the length of a Dictionary unfortunately you would have to create a function for it. One of the way is to iterate through itself and have a counter updated per iteration.

Here’s a post that talks about more in depth: I dont know what im doing wrong (dictionarys)

image

It doesn’t work for me, you can see me running the script in command bar (and it printed a empty table)

aka this is output for the commandbar:

require(game:GetService("ServerScriptService").ModuleScript).SeeData()  -  Studio
18:58:27.920  {}  -  Server - ModuleScript:4

this is output for the scripts:

  18:58:27.632   ▼  {
                    ["hello16857"] = true,
                    ["hello199408"] = true,
                    ["hello256539"] = true,
                    ["hello306021"] = true,
                    ["hello522135"] = true,
                    ["hello557237"] = true,
                    ["hello57227"] = true,
                    ["hello682846"] = true,
                    ["hello702433"] = true,
                    ["hello860975"] = true

image

After some digging it seems that the command line cannot access _G data which is an abbreviation of Global. Your variable module.Data = {} is currently being saved as a Global variable, which is why the command line is returning the table as empty. This post explains it well: Allow the Command Line to access the Client/Server's _G

1 Like

Thanks that helped me solve my issues with it, had this be a mental block for a while and now a lot of bugs that I experienced make sense

1 Like

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