For completeness, you can transfer information from one script to another using ModuleScripts:
3 | function Module.Add 1 (i) return i+ 1 end |
2 | local module = require(workspace.Module) |
6 | module.SpecialNumber = 9 |
2 | local module = require(workspace.Module) |
3 | print (module.SpecialNumber) |
5 | print (module.SpecialNumber) |
Note that, instead of modifying a Module with scripts (as was done with module.SpecialNumber = 9
), you should probably just change ModuleScript itself, or document that you intend to change the Module in a certain way. ex, a good comment to support changing SpecialNumber might be:
4 | function Module.Add 1 (i) return i+ 1 end |
That way, when you're reading through the ModuleScript later and have forgotten what you were doing with it, you won't have to also read through all your other scripts to figure out what this "SpecialNumber" is. And, if you later change the module to receive the user's favourite number, you will know not to call it "SpecialNumber".
In the same way that I've demonstrated transferring a number, you can transfer references to objects, tables, and functions.
So, say you have a function in Script1 that you want to call from Script2. One solution is to put that function into a ModuleScript (or modify the ModuleScript from within Script1 to include it, depending on that function's requirements -- ex, if it uses global variables from within Script1, you'd lose all those variables if you just moved the function to the ModuleScript.) Another solution is to move all of Script1 into a ModuleScript, and then put a small script as a child of that ModuleScript whose sole job is to require the ModuleScript (causing it to run like a normal script).
All of this information is only applicable on the same machine (ex content on the server is not automatically transferred to the client and vice versa). You will need to use RemoteEvents/RemoteFunctions if you want to do that. (Clients cannot directly communicate with each other.) Note that ModuleScripts only run once on any one machine, even if you require it numerous times.