Skip to content

Event

Event re-implements Roblox's event API (connect, fire, wait) by wrapping a BindableEvent.

This Event implementation is based on the Signal from Nevermore Engine by Quenty.

Why?

This implementation does not suffer from the restrictions normally introduced by using a BindableEvent. When firing a BindableEvent, the Roblox engine makes a copy of the values passed to BindableEvent:Fire. On the other hand, this class temporarily stores the values passed to fire, fires the wrapped BindableEvent without arguments. The values are then retrieved and passed appropriately.

This means that the exact same values passed to fire are sent to connected handler functions and also returned by wait, rather than copies. This includes tables with metatables, and other values that are normally not serializable by Roblox.

Usage

local zoneCapturedEvent = Event.new()

-- Hook up a handler function using connect
local function onZoneCaptured(teamName)
    print("The zone was captured by: " .. teamName)
end
zoneCapturedEvent:connect(onZoneCaptured)

-- Or use wait, if you like that sort of thing
local teamName
while true do
    teamName = zoneCapturedEvent:wait()
    print("The zone was captured by: " .. teamName)
end

-- Trigger the event using fire
zoneCapturedEvent:fire("Blue team")
zoneCapturedEvent:fire("Red team")

-- Remember to call cleanup then forget about the event when
-- it is no longer needed!
zoneCapturedEvent:cleanup()
zoneCapturedEvent = nil

Constructors

Event Event.new()

Constructs a new Event.

Functions

RBXScriptConnection Event:connect(function handler)

Connect a new handler function to the event.

Returns a connection object that can be disconnected.

... Event:wait()

Wait for fire to be called, then return the arguments it was given.

Event:fire(...)

Fire the event with the given arguments.

All handlers will be invoked. Handlers follow Roblox Event conventions.

Event:cleanup()

Disconnects all connected events to the Event.

Voids the Event as unusable.