Module · Scripting core
net
Messages between the client and server copies of your mod. Server handlers receive the sending Player first, then the data; client handlers receive the data. Data may be tables, strings, numbers, booleans, vec3/quat/color and Player objects. Messages from clients are rate-limited.
net.broadcast(channel: string, data?: any, reliable?: boolean, except?: Player)
Sends data to every player, optionally skipping one.
net.broadcast("announce", { text = "Round starts!" })net.handle(channel: string, schema: table<string, string>?, handler: function, options?: table): Subscription
net.on with the checks every handler needs built in. The data must match schema (a table of field = type: "string", "number", "integer", "boolean", "table", "vec3", "quat", "color", "Player" or "any"; a trailing ? makes a field optional, [] makes it an array, e.g. "string[]"; nil schema = any data). Server: handler(player, data); a message that does not match, or that goes over options.rate messages per second per player, never reaches it — the sender gets a reply on options.deny (default "denied") with { reason, channel }. Returning false, reason from the handler sends the same reply. Client: handler(data); bad messages are logged and dropped.
schema: Field = type, or nil for any data.options(optional): { rate = messages per second per player, burst = extra allowance (default rate), deny = reply channel }
-- server.lua
net.handle("move", { blocks = "string[]", offset = "vec3" }, function(player, data)
if #data.blocks > 64 then return false, "Too many blocks." end
-- … do the move …
end, { rate = 20 })net.isConnected(): boolean
True while in a session (hosting or connected to a server).
net.isHost(): boolean
True when this game hosts the session (singleplayer, or hosting for friends).
net.isServer(): boolean
True in server scripts.
net.on(channel: string, handler: function): Subscription
Handles messages on a channel. Server: handler(player, data). Client: handler(data).
-- server.lua
net.on("ping", function(player, data)
net.sendTo(player, "pong", { at = time.now() })
end)net.send(channel: string, data?: any, reliable?: boolean)
Sends data to the server copy of this mod.
reliable(optional): false for frequent, loss-tolerant updates.
net.send("ping", { hello = true })net.sendTo(player: Player, channel: string, data?: any, reliable?: boolean)
Sends data to one player's copy of this mod.