network.add_receive_hook

Observes incoming packets.

Syntax

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

Aliases

  • raknet.add_receive_hook
  • packets.add_receive_hook
  • RakNet.add_receive_hook
  • network.addreceivehook
  • raknet.addreceivehook
  • packets.addreceivehook
  • RakNet.addreceivehook

Parameters

ParameterTypeDescription
CallbackfunctionCalled as Callback(Packet) for each observed incoming packet.

Returns

TypeDescription
numberHook ID accepted by network.remove_receive_hook.

Notes

The callback receives a readonly packet view. Return values are ignored. This observer cannot change or cancel packets already received. Callback failures are reported by the executor. At most 32 send and receive hooks total can be registered.

Example

Luau
local Hook = network.add_receive_hook(function(Packet)
    if Packet.type ~= "Chat" then
        return
    end

    local Fields = Packet.decoded
    if Fields then
        print(Fields.username, Fields.text)
    else
        warn(Packet.decodeError)
    end

    -- Incoming hooks observe packets. They cannot edit or block them.
end)

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