Skip to content

Net

System · Net · server and client scripts

Net declares typed client-server messages and sends them between players and the server. Declare messages in a shared module with ToServer or ToClients, register handlers with On or Once, and send with Send, SendTo, SendAll, SendExcept, or SendNear.

ToServer

Function
ToServer(name: string, fields: { [string]: any }?, options: ServerMessageOptions?) -> ServerMsg<any>

ToServer declares a message sent from clients to the server and returns an opaque ServerMsg handle. fields is an optional field table (using Field helpers or typed defaults), and options accepts Reliable (default true) and Timing ("Arrival" or "Input"). In Field.List(Field.Entity), any entity not replicated to the sender resolves to nil at its original index, so iterate with a numeric loop rather than ipairs.

Example

-- Server script.
local Fire = Net.ToServer("Fire", {
    Origin = Vector3.Zero,
    Direction = Vector3.Zero,
}, {
    Reliable = true,
    Timing = "Input",
})

Net.On(Fire, function(player: Entity, data)
    print(player.Name, data.Origin)
end)

ToClients

Function
ToClients(name: string, fields: { [string]: any }?, options: ClientMessageOptions?) -> ClientMsg<any>

ToClients declares a message sent from the server to clients and returns an opaque ClientMsg handle. fields is an optional field table, and options accepts Reliable (default true).

Example

-- Server script.
local ScoreUpdated = Net.ToClients("ScoreUpdated", {
    Score = 0,
})

Net.SendAll(ScoreUpdated, { Score = 10 })

On

Function
On(msg: ServerMsg<any> | ClientMsg<any>, fn: (...any) -> ()) -> Connection

On registers a handler for a declared message and returns a Connection. On the server, the handler receives (player, data?) for a ServerMsg. On a client, the handler receives (data?) for a ClientMsg.

Example

-- Server script.
local Interact = Net.ToServer("Interact", {
    Target = Field.Entity,
})

local conn = Net.On(Interact, function(player: Entity, data)
    print(player.Name, data.Target)
end)
conn:Disconnect()

Once

Function
Once(msg: ServerMsg<any> | ClientMsg<any>, fn: (...any) -> ()) -> Connection

Once registers a one-shot handler for a declared message, disconnects it after the next matching delivery, and returns a Connection.

Example

-- Client script.
local MatchStarted = Net.ToClients("MatchStarted")

Net.Once(MatchStarted, function()
    print("match started")
end)

Send

FunctionClient only
Send(msg: ServerMsg<any>, data: { [string]: any }?) -> boolean

Send sends a ServerMsg from the local client to the server and returns true if the message fit in this tick's network budget.

Example

-- Client script.
local Ping = Net.ToServer("Ping", {
    Seq = 0,
})

if Net.Budget() > 0 then
    Net.Send(Ping, { Seq = 1 })
end

Budget

FunctionClient only
Budget() -> number

Budget returns the remaining per-tick outbound message byte budget on the local client.

Example

-- Client script.
print(Net.Budget())

SendTo

FunctionServer only
SendTo(target: Entity | { Entity }, msg: ClientMsg<any>, data: { [string]: any }?) -> boolean

SendTo sends a ClientMsg from the server to one player or a list of players and returns true when queued.

Example

-- Server script.
local Notice = Net.ToClients("Notice", {
    Text = "",
})

Component.User.Added:Connect(function(player: Entity)
    Net.SendTo(player, Notice, { Text = "Welcome" })
end)

SendAll

FunctionServer only
SendAll(msg: ClientMsg<any>, data: { [string]: any }?) -> boolean

SendAll sends a ClientMsg from the server to every connected player and returns true when queued.

Example

-- Server script.
local RoundEnded = Net.ToClients("RoundEnded")
Net.SendAll(RoundEnded)

SendExcept

FunctionServer only
SendExcept(excluded: Entity | { Entity }, msg: ClientMsg<any>, data: { [string]: any }?) -> boolean

SendExcept sends a ClientMsg from the server to every connected player except one player or a list of players and returns true when queued.

Example

-- Server script.
local Joined = Net.ToClients("Joined", {
    Name = "",
})

Component.User.Added:Connect(function(player: Entity)
    Net.SendExcept(player, Joined, { Name = player.Name })
end)

SendNear

FunctionServer only
SendNear(origin: vector | Entity, msg: ClientMsg<any>, data: { [string]: any }?) -> boolean

SendNear sends a ClientMsg from the server to every player whose spatial interest set covers origin (a Vector3 or an Entity) and returns true when queued.

Example

-- Server script in a part.
local Burst = Net.ToClients("Burst", {
    At = Vector3.Zero,
}, {
    Reliable = false,
})

local pos = self.Position
if pos then
    Net.SendNear(pos, Burst, { At = pos })
end