Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/Elitriare/ByteNet-Max/llms.txt

Use this file to discover all available pages before exploring further.

ByteNet Max provides four methods for reacting to incoming data on both packets and queries. Understanding the differences between them helps you write cleaner, safer networking code and avoid memory leaks caused by stale callbacks.
All four listener methods — listen, listenOnce, disconnectAll, and wait — work identically on both packets and queries. The examples below use packets, but the same patterns apply directly to queries.
listen(callback) registers a callback that fires every time the packet or query is received. It returns a disconnect function that you can call at any time to remove that specific listener.
local Packets = require(path.to.Packets)

-- Register a persistent listener
local disconnect = Packets.packets.UpdateHealth.listen(function(data, player)
    print(player.Name, "health:", data.health)
end)

-- Later, when you no longer need the listener:
disconnect()
The disconnect function removes only the listener it was created for. Other listeners registered with separate listen calls remain active.
listenOnce(callback) registers a callback that fires exactly once. The listener is automatically disconnected after it fires, so you do not need to call the returned disconnect function under normal conditions. The disconnect function is still returned in case you want to cancel before the first fire.
local Packets = require(path.to.Packets)

-- Fires once when the round starts, then auto-disconnects
Packets.packets.RoundStart.listenOnce(function(data, player)
    print("Round started! Mode:", data.mode)
end)
Cancelling before it fires:
local cancel = Packets.packets.RoundStart.listenOnce(function(data, player)
    print("This will not run if cancelled first")
end)

-- Prevent it from ever firing
cancel()
disconnectAll() removes every callback registered on that packet or query in a single call. It is equivalent to calling the disconnect function returned by every previous listen or listenOnce call.
local Packets = require(path.to.Packets)

Packets.packets.UpdatePosition.listen(function(data, player)
    -- listener A
end)

Packets.packets.UpdatePosition.listen(function(data, player)
    -- listener B
end)

-- Removes both listener A and listener B
Packets.packets.UpdatePosition.disconnectAll()
disconnectAll clears every listener on the packet or query — including ones registered by other systems. Only use it when you are certain you own all listeners on that packet, such as during a controlled teardown sequence.
wait() suspends the current coroutine until the packet or query fires once, then resumes it with the received data. It behaves like a one-shot listener implemented with coroutine.yield, so it does not block the rest of the script.
local Packets = require(path.to.Packets)

task.spawn(function()
    -- Yields here until the next UpdateHealth packet arrives
    local data, player = Packets.packets.UpdateHealth.wait()
    print("Received health update:", data.health)
end)
wait is useful for sequential flows where you want to pause execution until a specific event occurs, without setting up a persistent listener.

Disconnect pattern in practice

Assign the return value of listen to a variable so you can disconnect it later. This is the recommended approach for listeners that should be tied to the lifetime of a specific object or state:
local Packets = require(path.to.Packets)

local onHealthChanged

onHealthChanged = Packets.packets.UpdateHealth.listen(function(data, player)
    if data.health <= 0 then
        print(player.Name, "has died")
        -- Clean up this specific listener once its job is done
        onHealthChanged()
    end
end)
Disconnecting inside the callback is safe — ByteNet Max uses table.find and table.remove to locate the listener by reference, so removing it during iteration does not corrupt the listener list.

Build docs developers (and LLMs) love