Sharing signals
Should you be in doubt, here are the main ways to share signals between scripts:
Modules
You can return a table of signals in a ModuleScript, which multiple other scripts can then require and thereby access the same signals and communicate.
Scripts can also add or remove signals from the ModuleScript at any time.
Example:
-- "Signals" (ModuleScript)
local Signal = require(script.SignalPlus)
return {
CoolSignal = Signal()
SuperCoolSignal = Signal() :: Signal.Signal<number> -- Custom type :D
}
-- Some script
local signals = require(script.Signals)
local coolSignal = signals.CoolSignal
task.wait(5)
coolSignal:Fire()
-- Some other script
local signals = require(script.Signals)
local coolSignal = signals.CoolSignal
coolSignal:Connect(function()
print("CoolSignal fired!")
end)
Built-in shared table
You can store signals in the built-in shared table, which multiple other scripts can then access and thereby access the same signals and communicate.
Scripts can also add or remove signals from the shared table at any time.
Example:
-- Some script
local Signal = require(script.SignalPlus)
shared.CoolSignal = Signal()
task.wait(5)
shared.CoolSignal:Fire()
-- Some other script
-- Just make sure that this part runs after
-- the signal has been added to the shared table.
shared.CoolSignal:Connect(function()
print("CoolSignal fired!")
end)
Last updated