Accessing table variable using either side (Client or Server)?

So I have been working on something recently, and I’ve been experimenting with module scripts to test something. Now I am having trouble accessing variables.

What does the problem look like you ask me?
Well let’s say I have a module script in ReplicatedStorage. It contains a simple enough code. Looks like this:

local module = {
	ExampleVariable = 50
}

return module

Now I have a server script in ServerScriptService, what it’s gonna do is it’s gonna modify the variable. It changes the variable definition like this:

-- Modify the variable inside the table. I expected it to save for both sides.

local ModuleScript = require(game.ReplicatedStorage.ModuleScript)

wait(10)

ModuleScript.ExampleVariable = 100
print(ModuleScript.ExampleVariable)

Now it does change the “ExampleVariable” to 100 successfully and it prints in the output as 100 in server side just to make sure the variable changed.

Then I have a localscript in PlayerScripts, it will try and access the variable that has been modified by the server script I mentioned above. Looks like this:

-- Access the table from localscript using the same modulescript.
-- I'm expecting to get the same result in the output

local ModuleScript = require(game.ReplicatedStorage.ModuleScript)

while true do
	wait(.5)
	print(ModuleScript.ExampleVariable)
end

I use a loop to print the result anytime the script modifies the modulescript.

Now when it prints in the client side, it is still 50. It’s like it never saved for both sides even tho server can make changes to the entire game globally not locally. I’m so confused, if the server script changed the variable inside the table to 100 why is it not the same for client too?

Output results:

You can see for server, it’s 100. For client, it’s still 50 which it should have been 100 since it’s been modified through code as I mentioned.

You got any ideas or any suggestions? Tell me in the replies below.

1 Like

You can use ModuleScripts for sharing data between scripts, however the changes will not replicate past the client-server boundary, regardless if the changes to the data are made on the server. If you require a ModuleScript on both the client and server, they will both be different instances. For communicating across the client-server boundary, you should use RemoteEvents & RemoteFunctions.

3 Likes

Yo that’ was a quick solution. I tried it before you even replied so yeah, RemoteEvents are the solution for these kinds of stuff in Module Scripts. Thanks again for sharing more info about module scripts @m_xddle

1 Like