What happens when Server and Client both require a ModuleScript in ReplicatedStorage

Check my thinking:

If a client makes changes to the module, it won’t replicate to the server. If the server makes a change, it will replicate to all clients.

2 Likes

When a script / local script requires a ModuleScript, it essentially makes a copy of the ModuleScript for the script requiring the module. So, an alteration of a module script won’t replicate to others, regardless if it’s a server script or not

The actual behavior is that all scripts are different on every machine. A LocalScript on your client is not the same as the one in the same location on someone else’s client. This applies to ModuleScripts too.

The previous answer makes it a bit confusing near the end, so to clarify: a change to a ModuleScript’s return value is shared only on the same machine.

  • If you change the value on the server, all the server scripts on that server that access it see it.
  • If you change the value on the client, all the client scripts on that client that access it see it.
9 Likes

I module script never runs but the module inside it will run on local script if required by local script and the module will then be client but only for the local script if it was a server script the module will be for the server. Example: Server script require a module with a table of all the houses in the Map. Then that table will be for the server, so if you loop through it and change some properties it will happen on the server same goes for the client. If you require them on both scripts the table will be for client ont the local script but for the server in server script. The changes in the table on server will only apply to the server not the client same goes for the client

Correct. Through the use of exploits the client can theoretically do anything, but the server will reject source code changes among other things.

And if you do something like module.x = 5 on the client, it won’t replicate to the server unless you established some sort of auto-replication via RemoteEvents or what have you. And this is all your code doing this explicitly, Roblox does not do it for you.

Theoretically (speculatively), if the server made changes to the source code of the script, the new source code would replicate to clients (actually a compiled version that clients could run but not read). We know the server can compile and replicate scripts because InsertService is a thing. However, if the client already ran that ModuleScript before, requiring the same ModuleScript would not execute the modified code. ModuleScripts only ever run their code once, so the server would have to replace the module… you might as well use RemoteEvents to replicate whatever changes you need to the client, then have the client willingly make those changes to its data. There are libraries for replicated data structures out there, like Replica.

Additionally, you cannot modify the source code of ModuleScripts from the server anyway, you can only replace one with another one that has been pre-written/generated in Studio. On the Roblox server, there is no way to escalate identity levels like you can on the client.

When you require a ModuleScript on the client, that code is run on the client and you get whatever it returned. Same for the server. Making a change on the client or server would not do anything, same as if you made a table called a on a client and another table called a on the server, it would not replicate. You can manually replicate things via RemoteEvents/Functions or Instances.

Let me know if you have any questions

3 Likes

Ok that replica module looks sick thanks for showing me that.

Also I thought that since modulescripts only run once, if I change a variable inside a modulescript then other scripts with that module required can see that change. So I thought that would be replicated but thats not the case.

That’s true, but not across network boundaries. If two LocalScripts required the same module, both LocalScripts see the same object returned by the ModuleScript, and modifying that object is, well, modifying the object that both scripts have. So they both can see it.

But Scripts on the server can’t see it. That object was generated by code that ran on the client, and like all Lua data structures, it does not replicate. Only the client sees the changes that the client makes, and only the server sees changes that the server makes.

Replica works by asking the client to make matching changes whenever the server tells it to. :stuck_out_tongue:

1 Like

So to change a table inside a module, I can maybe call a function inside that module that makes changes in the table? It seems a pretty good idea for me.