network.add_send_hook

Intercepts outgoing packets.

Syntax

Signature
network.add_send_hook(Callback: function) -> number

Aliases

  • raknet.add_send_hook
  • packets.add_send_hook
  • RakNet.add_send_hook
  • network.addsendhook
  • raknet.addsendhook
  • packets.addsendhook
  • RakNet.addsendhook

Parameters

ParameterTypeDescription
CallbackfunctionCalled as Callback(Packet) for each intercepted outgoing packet.

Returns

TypeDescription
numberHook ID accepted by network.remove_send_hook.

Notes

The callback receives an editable Packet. Return false or call Packet:Block() to discard it. Return a replacement payload to replace it. Returning nil or true keeps the packet, including changes made with SetData or SetDecoded. Callbacks must not yield. A callback error, yield, invalid replacement, or exceeded pump budget discards the packet. At most 32 hooks total can be registered.

Example

Luau
-- Observe outgoing chat packets without changing them.
local Hook = network.add_send_hook(function(Packet)
    if Packet.type ~= "Chat" then
        return
    end

    local Fields = Packet.decoded
    if Fields then
        print("Outgoing chat:", Fields.text)
    else
        warn(Packet.decodeError)
    end

    -- Returning nothing leaves the packet unchanged.
end)

task.delay(15, function()
    network.remove_send_hook(Hook)
end)