Signal
Signals allow events to be dispatched and handled.
For example:
local signal = Signal.new()
signal:Connect(function(msg)
print("Got message:", msg)
end)
signal:Fire("Hello world!")
Types
SignalConnection
Represents a connection to a signal.
local connection = signal:Connect(function() end)
print(connection.Connected) --> true
connection:Disconnect()
print(connection.Connected) --> false
ConnectionFn
type
ConnectionFn =
(
...any
)
→
(
)
A function connected to a signal.
Functions
new
Constructs a new Signal
Wrap
Constructs a new Signal that wraps around an RBXScriptSignal.
For example:
local signal = Signal.Wrap(workspace.ChildAdded)
signal:Connect(function(part) print(part.Name .. " added") end)
Instance.new("Part").Parent = workspace
Is
Signal.
Is
(
obj:
any
--
Object to check
) →
boolean
--
true
if the object is a Signal.
Checks if the given object is a Signal.
Connect
Connects a function to the signal, which will be called anytime the signal is fired.
signal:Connect(function(msg, num)
print(msg, num)
end)
signal:Fire("Hello", 25)
Once
Connects a function to the signal, which will be called the next time the signal fires. Once the connection is triggered, it will disconnect itself.
signal:Once(function(msg, num)
print(msg, num)
end)
signal:Fire("Hello", 25)
signal:Fire("This message will not go through", 10)
DisconnectAll
Signal:
DisconnectAll
(
) →
(
)
Disconnects all connections from the signal.
signal:DisconnectAll()
Fire
Signal:
Fire
(
...:
any
) →
(
)
Fire the signal, which will call all of the connected functions with the given arguments.
signal:Fire("Hello")
-- Any number of arguments can be fired:
signal:Fire("Hello", 32, {Test = "Test"}, true)
FireDeferred
Signal:
FireDeferred
(
...:
any
) →
(
)
Same as Fire
, but uses task.defer
internally & doesn't take advantage of thread reuse.
signal:FireDeferred("Hello")
Wait
This is a yielding function. When called, it will pause the Lua thread that called the function until a result is ready to be returned, without interrupting other scripts. YieldsSignal:
Wait
(
) →
...
any
Yields the current thread until the signal is fired, and returns the arguments fired from the signal.
Yielding the current thread is not always desirable. If the desire is to only capture the next event
fired, using Once
might be a better solution.
task.spawn(function()
local msg, num = signal:Wait()
print(msg, num) --> "Hello", 32
end)
signal:Fire("Hello", 32)
Destroy
Signal:
Destroy
(
) →
(
)
Cleans up the signal.
Technically, this is only necessary if the signal is created using
Signal.Wrap
. Connections should be properly GC'd once the signal
is no longer referenced anywhere. However, it is still good practice
to include ways to strictly clean up resources. Calling Destroy
on a signal will also disconnect all connections immediately.
signal:Destroy()
ConnectOnce
This was deprecated in v1.3.0
Use `Signal:Once` instead.