Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do I fire a function from a different script?

Asked by 7 years ago
Edited 7 years ago

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?

2 answers

Log in to vote
4
Answered by 7 years ago

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.

0
Sorry for the late response. This answer is great! Thanks so much! User#15461 0 — 7y
Ad
Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

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)

0
Not really what I was looking for but ok User#15461 0 — 7y
0
What were you looking for? fireboltofdeathalt 118 — 7y
0
You can't use _G for localscripts; I recommend using ModuleScripts instead; for _G, script B would have to wait for _G.VARIABLE to load, otherwise it'll error depending on what you're doing. TheeDeathCaster 2368 — 7y
0
You can use _G in localscripts. fireboltofdeathalt 118 — 7y
View all comments (2 more)
0
@fireboltofdeathalt True, but it doesn't commute between other localscripts; server-sides only have that ability/ advantage. TheeDeathCaster 2368 — 7y
1
Modules are WAYYYY more ideal >>; Vingam_Securis 213 — 7y

Answer this question