Im trying to learn more about Module Scripts and I'm getting confused as to how it communicates with the client/server

I’m specifically trying to learn about variables in module scripts and how its saved from 1 script to another (if its used that way), here’s an example

the gist of the code is it loads the animations I have from an animation folder in ReplicatedStorage and loads it into the animator of the player, while also inserting the animation into a table called “LoadedAnimations”

Code Sample 1

local AnimationHanlder = require(game:GetService("ReplicatedStorage").AnimationHandler)
AnimationHanlder:LoadAnimations(game.Players.LocalPlayer)
print(AnimationHanlder.LoadedAnimations)

Once printed, you would expect the LoadedAnimations to print, and that is exactly what happens

Come Sample 2

local AnimationHanlder = require(game:GetService("ReplicatedStorage").AnimationHandler)
wait(1)
print(AnimationHanlder.LoadedAnimations)

now this is the same just without loading the animations, meaning “LoadedAnimations” table is empty,

But if Code Sample 1 and Code Sample 2 run simultaneously, Code Sample 2 prints the loaded animations, which was only loaded in Code Sample 1

So how does that work, how does the module script (save i guess?) to different scripts each requiring it again, how does it communicate to each different client differently?

And also how would this work differently if used in OOP, aka metatables and all that thingy mijgy.

Thanks in advance.

When you require() a ModuleScript, you are essentially grabbing its contents, which is getting an Array in return. When you require() a ModuleScript on the Client, The ModuleScript will run in that Environment, so all the Changes to the ModuleScript on the Client would only effect the Client, which is essentially the Players view of the Game.

Same can be said with the Server, but the Changes their effect the entire ModuleScript, and on every Client, and Any changes done on the Client will not be Replicated unless told to, by a RemoteEvent or RemoteFunction

1 Like

The part about the client only replicating module script changes to the local player is correct, however, the server only makes changes on the SERVER

If you change something in a module script on the server it won’t replicate to all clients, only the server itself.

Basically, if two script from the same environment require the module script, the same object (table in most cases) will be returned and the scripts will share the same version of the module

If two script require a module script from different environments (client,server, or under actors for parallel lua), they will get different versions of the module

The Server makes changes to both Clients, and the Server.

Not in the case of module scripts, the server changes module scripts only in its own environment.

As for other applications, like the workspace, yes it would replicate to clients.

2 Likes

No, youre right.

Because when requiring a Module, you are essentially calling it, to return a table under that environment, Reminds me of _G where its only accessable under the context level it was created in.

(I have no idea what I said here)

So i only ever use module scripts for OOP where i have a .new() function creating a new object with all the functions in this case two scripts would not be able to access the same objects,
so essentially, that means that the module script itself is the object in the case its not being used for OOP, so requiring means I’m accessing “That” object and any script who tries requiring the script would also get access to “That” object and any changes done to it right?
and changing “That” object in the client only changes “That” object for that client but if its changed in the server all clients would access the changes that happened to “That” object correct?

Not sure what you are saying, but multiple scripts can have access to the same object (if that is what you are asking)

The issue lies however when trying to transfer that object between environments (Client → Server or Server → Client). In that case no, you cannot share the same object between the server and client.

The other issue would be if you attempt to fire the object through a BindableEvent, it won’t preserve the functions of the metatable it is contained in. But other than that, you could have modules storing objects to use between different scripts in the same environment.

1 Like

What i was trying to say was the Module Script table which is being returned, in the case of the just 1 client right now, is the object that is being called when requiring, so all client scripts that call it on that “1” client access the same object, including all changes happening to that object.

Well, upon re-reading your question you asked if the server would affect the object on all clients, and it won’t. The modules are only stored in their respective environment.

But if you have for example, a part or another instance stored within your object (i.e. self.Part = workspace.Part) and you edit it through that object (i.e. self.Part.BrickColor = BrickColor) then yes, it would replicate.

Ohh okay i understand this

but is this statement true?

Yes, if the client creates the object and other client-sided scripts use things on that client, it’ll replicate on that object. Of course for the local player only.

ok thanks alot i understand module scripts a bit better now! :D.

1 Like

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