Just wondering, is it possible to fire a function for a different script? I guess I could use a remote event... but is there a better way to do it?
For completeness, you can transfer information from one script to another using ModuleScripts:
--ModuleScript named "Module" with parent workspace Module = {} function Module.Add1(i) return i+1 end --Add other functions and information to Module here return Module
--Script local module = require(workspace.Module) print(module.Add1(5)) --6 --You can modify module as well, send information to it that can be read by other scripts, etc. ex: wait(0.1) module.SpecialNumber = 9
--Script2 local module = require(workspace.Module) print(module.SpecialNumber) --will print 'nil' wait(0.2) print(module.SpecialNumber) --will print '9'
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:
--ModuleScript named "Module" with parent workspace Module = {} --Module.SpecialNumber can be changed by scripts to store the special number given to the user function Module.Add1(i) return i+1 end return Module
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.
I don't quite understand what you mean. If you mean Local to Server, that's not possible. You could use _G for server-server or client-client
--Script A _G.A = function(m) print(m) end
--Script B _G.A("Woof") => Output: Woof
There might be other ways to do with with BindableEvents/BindableFunctions
If you wanna call something on Script B
but have it fire on script A
You'll have to use something like BindableEvents/BindableFunctions (Server-Server:Client-Client)