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:
const Signal = require(path.to.module)
const helloEvent = Signal.new()Type Annotation
To utilize NamedSignal's main quality-of-life feature — named parameters with full type safety — you should annotate the type of the Signal.
You can do this in several ways:
const helloEvent: Signal.Signal<(subject: string) -> ()> = Signal.new()const helloEvent = Signal.new() :: Signal.Signal<(subject: string) -> ()>const helloEvent = Signal.new<<(subject: string) -> ()>>()All three approaches achieve the same result, use whichever fits your requirements.
Connect a Listener
Once typed, Luau can automatically fill in the connecting function for you:
const helloConnection = helloEvent:Connect(function(subject: string)
print(`Hello, {subject}!`)
end)Signal:Connect() returns a Connection object, which can later be used to disconnect the listener by calling helloConnection:Disconnect().
Fire the Signal
Trigger the event by calling Signal:Fire() with the expected arguments:
helloEvent:Fire("world")Full Example
All together:
const Signal = require(path.to.module)
const helloEvent = Signal.new<<(subject: string) -> ()>>()
const 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.
const Module = {}
Module.fooEvent = Signal.new<<(cat: "meow") -> ()>>()
return ModuleOther scripts can then access the Signal by requiring the module that contains it.