Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

External changes made to a Module Script are local to the script that made the change, why is this?

Asked by 6 years ago

So, in my game, I have recently seen that it would be convenient to store a certain amount of data within a global table inside a module script. Everything with this idea was going well up until the point where there was a problem. Initially, the origin of this problem confused me and I was stumped for a few days. I even tried looking on the official wiki page on module scripts and found nothing relevant to the solution of my problem. I decided to do a bit of experimentation with module scripts in general and discovered something quite strange; if a script were to change something within a module script, say, a table as I was attempting to use, those changes stay local to the script itself. In other words, if I were to make a change to a table within a module script from a server script, only that server script would be able to view those changes. Here is a scripted example of how this issue affects me:

Module Script

local module = {}

    module.ExampleTable = {}

return module

Server Script

local ServerModule = require(workspace.ModuleScript)

while wait(1) do 
    table.insert(ServerModule.ExampleTable, 1)
    print("Server"..#ServerModule.ExampleTable) 
    --[[ Since the table is being changed by a server script, all other server scripts will be able to see/access these changes. However, local scripts will still be viewing ExampleTable as completely unchanged and empty. ]]
end

Local Script

local ServerModule = require(workspace.ModuleScript)

while wait(1) do
    print("Local"..#ServerModule.abc) --[[ This will always be printing "0" in the output due to the fact that there have been no changes to ExampleTable from this local script. And the weirdest thing about this is that all changes made to the game in general from a server script should be accessible from all types of scripts, am I incorrect?  ]]
end

Does anyone know why changes made by the server are not viewable from local scripts? I am completely clueless on this one. Anyway, any help is greatly appreciated. Thanks for your time!

Answer this question