Subscribe
Register a callback for a named event. On fires every time the event arrives; Once fires only the first time. Both return an id you can pass to Off to remove the listener.
On
| Parameter | Type | Description |
|---|---|---|
event | string | Event name — must match the name used in app:Push(event, ...) or broadcast:Emit(event, ...) on the server. |
callback | (data: any) → () | Called every time the named event arrives on either the push or broadcast channel. |
Returns a subscription id (number) that can be passed to listener:Off(id) to remove the subscription.
local id = listener:On("round.start", function(data)
print("Round started, duration:", data.duration)
HUD:ShowTimer(data.duration)
end)
Once
Identical to On but automatically unsubscribes after the callback fires once. Use it when you only need to react to the first occurrence of an event — for example, waiting for initial player data to arrive.
-- fires once, then cleans itself up
listener:Once("player.data", function(data)
UI:SetCoins(data.coins)
UI:SetLevel(data.level)
end)
Difference between On and Once
| On | Once | |
|---|---|---|
| Fires | Every time the event arrives | Only the first time |
| Auto-unsubscribes | No — call Off manually | Yes — removed after first fire |
| Returns | id for Off | id for Off (before it fires) |
| Use case | Persistent listeners (round timer, health bar) | One-time setup (initial load, spawn event) |
Off
Removes a subscription by the id returned from On or Once. Safe to call after the subscription has already fired (Once) or never registered (no-op).
local id = listener:On("round.end", function(data)
print("Winner:", data.winner)
end)
-- Remove when the UI is closed
closeButton.Activated:Connect(function()
listener:Off(id)
end)
Callback receives data
The callback's first argument is whatever the server passed as the second argument to app:Push(event, data) or broadcast:Emit(event, data). It can be any Roblox-serialisable value — a table, number, string, etc.
-- server sends: app:Push("round.end", { winner = "Alice", time = 94.3 })
listener:On("round.end", function(data)
print(data.winner, data.time) -- "Alice" 94.3
end)
See also
← Listener · Server Push | how push events arrive · Broadcast | how broadcast events arrive · App Push | server-side push API