My own Signal module

-- Signal.lua
--!strict
local Signal = {}

Signal.__index = Signal

local Connection = {}

Connection.__index = Connection

type Connection = typeof(setmetatable({} :: {
	signal: Signal,
	_callback: any
}, Connection))

type Signal = typeof(setmetatable({} :: {
	_Connections: {Connection}
}, Signal))

function Connection.New(signal: Signal, callback: (...any) -> ()): Connection
	local _Connection = {
		signal = signal,
		_callback = callback
	}
	setmetatable(_Connection, Connection)
	table.insert(signal._Connections, _Connection)
	return _Connection
end

function Connection.Disconnect(self: Connection): ()
	local connections = self.signal._Connections
	for index, value in ipairs(connections) do
		if value == self then
			table.remove(connections, index)
			break
		end
	end
end

function Signal.New(): Signal
	return setmetatable({
		_Connections = {}
	}, Signal) :: any
end

function Signal.Destroy(self: Signal): ()
	setmetatable(self :: any, nil)
	table.clear(self._Connections)
end

function Signal.Connect(self: Signal, callback: (...any) -> ()): Connection
	return Connection.New(self, callback)
end

function Signal.Once(self: Signal, callback: (...any) -> ()): Connection
	local Connection: Connection
	local HasFired: boolean = false
	Connection = self:Connect(function(...: any)
		if not HasFired then
			HasFired = true
			Connection:Disconnect()
			task.defer(callback, ...)
		end
	end)
	return Connection
end

function Signal.Wait(self: Signal): ...any
	local CurrentThread = coroutine.running()
	self:Once(function(...: any)
		coroutine.resume(CurrentThread, ...)
	end)
	return coroutine.yield()
end

function Signal.Fire(self: Signal, ...: any): ()
	for _, Connection in ipairs(table.clone(self._Connections)) do
		local callback: (...any) -> () = Connection._callback
		if callback then
			task.defer(callback, ...)
		end
	end
end

function Signal.GetConnections(self: Signal): {Connection}
	return table.clone(self._Connections)
end

return Signal

Any feedback would be appreciated!

Why are you even writing a Signal module in 2025, bro? :skull:
Signals only make sense on the instance side; building your whole game logic around them is choosing pain for fun.

And then the implementation… bruh:

  • no thread pooling
  • metatable OOP (slow + unnecessary)
  • ipairs gets compiled into outdated opcode; use generic iteration instead.
  • Runtime self-protection like this is Java, not Luau.
  • Cloning the connections table every time, like memory is free

If you wanted a constructor-only module, just return the constructor instead of a full list of methods you never touch again.

Signals are only worth using if you’re making a DLC/modding architecture with feature flags and late-loaded assets. And even then, you’d hardcode handlers instead of spamming threads.

Nice effort, wrong tool.

Thank you, dude. And I have a question; if I want to use signal such as BindableEvent, what would you recommend me to use? (Sorry if it’s a silly question; I’m not good at programming.)

You honestly should not use BindableEvent, neither attempting to make own implementation
I suggest focusing game around functional programming and ECS entirely, its the most optimal solution for having good performance and ECS makes it scalable.

BindableEvent’s use in 2025 is purely cross VM communication (parallel Luau and experimental script capability feature)

Basically things that meant to be hardcodded should be, things that are not should be made with ECS

I’ve never heard of ECS before. Where can I learn more about it?