Quick Start
This guide shows the basics of using NamedSignal — creating, connecting, and firing signals.
Require and Create
Requiring the module returns a table with a new constructor:
local Signal = require(path.to.module)
local helloEvent = Signal.new()Type Annotation
To utilize NamedSignal's main quality-of-life feature, named parameters, you should annotate the type of the Signal.
You can do this in several ways:
local helloEvent: Signal.Signal<(subject: string) -> ()> = Signal.new() :: anylocal helloEvent = Signal.new() :: Signal.Signal<(subject: string) -> ()>local helloEvent = Signal.new<<(subject: string) -> ()>>()All three approaches achieve the same result, use whichever fits your requirements.
[*] Colloquial name for explicit type parameter instantiation, borrowed from Rust (::<T>).
Connect a Listener
Once typed, Luau can automatically fill in the connecting function for you:
local helloConnection = helloEvent:Connect(function(subject: string)
print(`Hello, {subject}!`)
end)TIP: Connection Lifecycle
Signal:Connect() returns a Connection object, which can later be used to disconnect the listener by calling Connection:Disconnect().
Fire the Signal
Trigger the event by calling Signal:Fire() with the expected arguments:
helloEvent:Fire("world")Full Example
All together:
local Signal = require(path.to.module)
local helloEvent = Signal.new<<(subject: string) -> ()>>()
local helloConnection = helloEvent:Connect(function(subject: string)
print(`Hello, {subject}!`)
end)
helloEvent:Fire("world")And voilà! You should get the following output when running the script:
Hello, world!Going Cross-Script
The most common way of sharing signals across scripts is to place it inside a table, whether at the module-level, as a member in a class, or elsewhere:
local Module = {}
Module.fooEvent = Signal.new<<(cat: "meow") -> ()>>()
return ModuleOther scripts can then access the created event by requiring the module that contains it:
local Module = require(path.to.module)
Module.fooEvent:Connect(function(cat: "meow")
print(cat)
end)