September 20, 2022


PackageHandlers:Receive(packetName, callback)


Receive the protocol from the other end. The receiving function only needs to be written once, and the callback function will be executed every time the corresponding packetname protocol is received.


Variable name Type Describe
packetName string Protocol name, corresponding to the receiving function
callback table Callback function, which is executed after receiving the protocol

The passed in player entity must be a player entity

Callback Arguments

Variable name Type Describe
player Entity Player entity receiving the protocol
packet table<variant> Parameters carried by the protocol

Code example

The server sends the "testpackage" protocol with two parameters

-- Player is a player entity, and the acquisition process is omitted here

-- Carried Parameters
local packet = {
    a = 1,
    b = 2

-- Send Protocol
PackageHandlers:SendToClient(player, "TestPackage", packet)

Client receives "testpackage" protocol

-- receive protocol
-- player : client player entity
-- packet : The parameters that the server carries when sending the protocol
PackageHandlers:Receive("TestPackage", function(player, packet)
     print("client receive package", player)
     print(packet.a, packet.b)