Signal (better but cooler)

Cooler Signal — Lightweight & Feature-Rich Signal Module

A lightweight, optimized Signal implementation for Roblox/Luau.

The goal is to provide a simple and flexible signal system that combines the functionality of a BindableEvent and BindableFunction into one module, while also providing additional features such as Once(), Wait(), timeouts, connection management, and Luau generic type support.

Features

  • :high_voltage: Lightweight and performance-oriented
  • :satellite_antenna: Asynchronous signal dispatch with :Fire()
  • :counterclockwise_arrows_button: Synchronous callback invocation with :Invoke()
  • :index_pointing_up: One-time connections with :Once()
  • :hourglass_not_done: Yielding with :Wait()
  • :stopwatch: Optional :Wait() timeouts
  • :electric_plug: Individual connection management
  • :broom: :DisconnectAll() and :Destroy()
  • :memo: Full Luau generic type support
  • :1234: Supports multiple signal arguments

Usage

Creating a Signal

local Signal = require("./Signal")

local onFoodEat = Signal.new()

You can then connect callbacks to it:

local connection = onFoodEat:Connect(function(food: string, amount: number)
	print(food, amount)
end)

onFoodEat:Fire("Apple", 25)

Output:

Apple 25

Connect() returns a SignalConnection that can be disconnected whenever you no longer need it:

connection:Disconnect()

Once

Use :Once() when you only want a callback to run once.

local connection = onFoodEat:Once(function(food: string, amount: number)
	print(food, amount)
end)

onFoodEat:Fire("Apple", 25)
onFoodEat:Fire("Banana", 10)

The callback will only run for the first Fire().

Once() also returns a SignalConnection:

connection:Disconnect()

Fire

:Fire() dispatches the provided arguments to every connected callback.

onFoodEat:Fire("Apple", 25)

Callbacks connected through :Connect() are executed asynchronously.

Fire() also returns the arguments that were passed to it:

local food, amount = onFoodEat:Fire("Apple", 25)

print(food)   -- Apple
print(amount) -- 25

This makes Fire() useful when you want normal event-style behavior.


Wait

:Wait() yields the current thread until the signal is fired.

local Signal = require("./Signal")

local onFoodEat = Signal.new()

task.delay(1, function()
	onFoodEat:Fire("Apple", 25)
end)

local food, amount = onFoodEat:Wait()

print(food, amount)

You can also provide a timeout:

local food, amount = onFoodEat:Wait(10)

If the timeout expires before the signal is fired, the waiting thread resumes without signal arguments.

This can be useful when you don’t want a thread waiting indefinitely.


Invoke

:Invoke() provides synchronous callback invocation.

Unlike :Fire(), callbacks are executed immediately and their return values are collected into a table.

local Signal = require("./Signal")

local onFoodEat = Signal.new()

onFoodEat:Connect(function(randomNumber: number)
	return {
		Food = "Apple",
		Amount = 6 + randomNumber,
	}
end)

onFoodEat:Connect(function(randomNumber: number)
	return {
		Food = "Banana",
		Amount = 7 + randomNumber,
	}
end)

local responses = onFoodEat:Invoke(67)

print(responses)

Output:

{
	{
		Food = "Apple",
		Amount = 73,
	},
	{
		Food = "Banana",
		Amount = 74,
	},
}

Each callback’s return value is added to the responses table in connection order.

For example:

local signal = Signal.new()

signal:Connect(function(value: number)
	return value + 10
end)

signal:Connect(function(value: number)
	return tostring(value)
end)

local responses = signal:Invoke(5)

-- {
--     15,
--     "5"
-- }

This makes Invoke() behave similarly to a BindableFunction while allowing multiple callbacks to respond.


Fire vs Invoke

Method Execution Returns
:Fire(...) Asynchronous Original arguments
:Invoke(...) Synchronous Table of callback results

Use Fire() when you want event-style communication.

Use Invoke() when you want to synchronously execute every callback and collect their return values.


Connections

Every Connect() and Once() call returns a SignalConnection.

local connection = onFoodEat:Connect(function(food)
	print(food)
end)

Disconnect it with:

connection:Disconnect()

Once disconnected, the connection will no longer receive future signal calls.

You can also check its connection state:

if connection.IsConnected then
	print("Still connected!")
end

DisconnectAll

To disconnect every connection from a signal:

onFoodEat:DisconnectAll()

This keeps the Signal object itself alive, allowing you to connect new callbacks later.

onFoodEat:DisconnectAll()

onFoodEat:Connect(function(food)
	print(food)
end)

Destroy

When the signal is permanently no longer needed, use:

onFoodEat:Destroy()

Destroy() disconnects all connections and destroys the signal’s metatable.

After calling Destroy(), the signal should no longer be used.


Type Annotations

The module provides generic Luau types for both Signals and SignalConnections.

The exported types are:

Signal.Signal<T...>
Signal.SignalConnection<T...>

Typing a Signal

You can specify the arguments a signal accepts:

local Signal = require("./Signal")

local onFoodEat: Signal.Signal<string, number> = Signal.new()

This tells Luau that the signal accepts:

string, number

So this is valid:

onFoodEat:Fire("Apple", 25)

while this should produce a type error:

onFoodEat:Fire(25, "Apple")

Typed Callbacks

The signal’s generic arguments are also reflected when connecting callbacks:

local onFoodEat: Signal.Signal<string, number> = Signal.new()

onFoodEat:Connect(function(food, amount)
	print(food)   -- string
	print(amount) -- number
end)

You can also explicitly annotate the callback:

onFoodEat:Connect(function(food: string, amount: number)
	print(food, amount)
end)

Typing a Connection

Connections can be explicitly typed as well:

local onFoodEat: Signal.Signal<string, number> = Signal.new()

local connection: Signal.SignalConnection<string, number> =
	onFoodEat:Connect(function(food: string, amount: number)
		print(food, amount)
	end)

You can then manage the connection normally:

connection:Disconnect()

Multiple Arguments

Signals support any number of generic arguments.

local playerJoined: Signal.Signal<Player, string, number> = Signal.new()

playerJoined:Connect(function(
	player: Player,
	username: string,
	joinTime: number
)
	print(player, username, joinTime)
end)

playerJoined:Fire(player, "wyverndayo", os.time())

The same argument types are preserved through Fire(), Invoke(), Wait(), Connect(), and Once().


Typing Wait

The generic signal arguments are also reflected by Wait():

local onFoodEat: Signal.Signal<string, number> = Signal.new()

local food, amount = onFoodEat:Wait()

Luau knows that:

food   -> string
amount -> number

You can also use a timeout:

local food, amount = onFoodEat:Wait(10)

Typing Invoke

Invoke() returns a table containing the return value of each callback.

local signal: Signal.Signal<number> = Signal.new()

signal:Connect(function(value: number)
	return value + 10
end)

signal:Connect(function(value: number)
	return tostring(value)
end)

local responses = signal:Invoke(5)

The resulting table is:

{
	15,
	"5",
}

Since callbacks can return arbitrary values, Invoke() returns:

{any}

API

Signal.new<T...>()

Creates a new Signal.

local signal = Signal.new()

Generic arguments can be specified:

local signal: Signal.Signal<string, number> = Signal.new()

Signal:Connect(callback)

Connects a callback to the Signal.

local connection = signal:Connect(function(...)
	-- ...
end)

Returns:

SignalConnection<T...>

Signal:Once(callback)

Creates a connection that automatically disconnects after its first invocation.

signal:Once(function(...)
	-- Runs once
end)

Returns:

SignalConnection<T...>

Signal:Fire(...)

Asynchronously invokes all connected callbacks.

signal:Fire(...)

Returns the original arguments.


Signal:Invoke(...)

Synchronously invokes all connected callbacks and collects their return values.

local responses = signal:Invoke(...)

Returns:

{any}

Signal:Wait(timeout?)

Yields until the Signal is fired.

local a, b = signal:Wait()

An optional timeout can be provided:

local a, b = signal:Wait(10)

Signal:DisconnectAll()

Disconnects every connection attached to the Signal.

signal:DisconnectAll()

Signal:Destroy()

Disconnects all connections and destroys the Signal.

signal:Destroy()

SignalConnection:Disconnect()

Disconnects an individual connection.

local connection = signal:Connect(callback)

connection:Disconnect()

Performance

The module is designed with performance in mind and keeps the implementation small and lightweight.

Signal.rbxmx (5.2 KB)

6 Likes

Isn’t the Once method supposed to clean itself up or am i tweaking?

It does but if you want to disconnect it so it doesnt fire.

Need any help I am here to help!

I don’t get it, the Once method is supposed to run once and disconnect itself no? so why are we disconnecting again

Edit: Ohh you meant so you could disconnect it before it fires, i get it now