Network protocol
Network sends tables between main.lua and server.lua. Define a small protocol instead of accumulating unrelated fields.
Always include kind
Section titled “Always include kind”Network:SendTable({ kind = "settings", enabled = true, threshold = 30})The receiver dispatches by message type first:
Network:OnTableReceived(function(data) if data.kind == "settings" then -- Apply settings elseif data.kind == "request_state" then -- Send state endend)Separate commands from state
Section titled “Separate commands from state”A useful naming scheme:
set_settings: client supplies settings;request_state: client requests a snapshot;state: server sends authoritative state;event: server reports something that happened.
SendTable() returns nil, not a response. A response is a separate incoming message.
Supported data
Section titled “Supported data”Send strings, numbers, booleans, nested tables, and sequential tables. Break structures into numbers:
local function PackVector3(value) return { x = value.x, y = value.y, z = value.z }endVector3, Agent, Texture, functions, and nil fields are not preserved.
Validate server input
Section titled “Validate server input”The sandbox does not expose type, so use safe conversions and explicit comparisons:
local function ApplySettings(data) local threshold = tonumber(data.threshold)
if threshold ~= nil then threshold = math.max(1, math.min(100, threshold)) State.threshold = threshold end
State.enabled = data.enabled == trueendNever use a client number as an index, distance, or duration without bounds.
Do not send every frame
Section titled “Do not send every frame”Send when state changes:
local LastHealth = nil
local function SendHealthIfChanged(health) if health == LastHealth then return end
LastHealth = health Network:SendTable({ kind = "health_state", health = health })endInitial synchronization
Section titled “Initial synchronization”Both sides should recover regardless of startup order:
- the server starts with safe defaults;
- the client registers its receiver;
- the client sends settings;
- the client requests state;
- the server responds with a full snapshot.
See the complete Reflex module for the full pattern.