Skip to content

Networking ​

Multiplayer ​

The same project runs on the server and on every client. Scripts check which side they're on.

sh
Gamma serve MyGame                                # server on port 53640
Gamma play --connect 192.168.1.20:53640 MyGame    # a client
luau
local chat = Scene.Remotes.Chat

if Network:IsServer() then
	chat.OnServerEvent:Connect(function(player, text)
		chat:FireAllClients(player.Name, text)
	end)
else
	chat.OnClientEvent:Connect(function(name, text)
		print(`{name}: {text}`)
	end)
	Network.Connected:Wait()
	chat:FireServer("hello")
end

Traffic goes over ENet (reliable UDP), so every message arrives, in order. Both sides need the Network permission. The web build can't be a server or client yet.

Starting ​

From the command line: Gamma serve [--port <N>] <Folder>, or --serve <Port> / --connect <Host:Port> on run and play. The game is already a server or client when scripts load, so Network:IsServer() works on the first line, and run keeps going while connected.

From a script:

MemberDoes
Network:StartServer(port?)Listens, and returns the port (a free one if left out)
Network:Connect(address, port)Connects to a server
Network:IsServer(), Network:IsClient()Which side this is
Network:Stop()Closes the server, or disconnects
Network.ConnectedClient: fires once its player exists
Network.Disconnected(reason)Client: the connection ended, the server was unreachable for about 5 s, or it was kicked

Players ​

The server makes a Player in Players for each client, named Player<UserId>. UserId counts from 1 in connection order. Every client gets a copy of every player.

  • Players.LocalPlayer: the client's own player, nil on the server
  • Players:GetPlayers(), Players:GetPlayerByUserId(id)
  • PlayerAdded, PlayerRemoving
  • player:Kick(message) on the server

Replication ​

When a client connects, the server sends it the whole Scene before Connected fires. After that, every step it sends what changed: new instances (with every property a place saves), changed properties, reparenting and removals. References between instances, like a weld's Part0, point at the client's copies.

  • If the client already has an instance with the same name, class and parent, it's reused. So a remote created on both sides stays one instance.
  • Cameras aren't copied; each client has its own.
  • Changes a client makes stay on that client.
  • Attributes aren't replicated yet.

Network ownership ​

The server simulates every part, and clients just show where the server says they are. part:SetNetworkOwner(player) (on the server) hands a part to that client instead: the client simulates it, moves it with no delay, and reports its position to the server, which passes it on.

That's how a character moves without lag: give its part to its player, and the client's CharacterController moves it straight away. SetNetworkOwner(nil), or the owner leaving, gives it back. GetNetworkOwner() says who has it. The server trusts the owner's positions and doesn't check for cheating.

Remotes ​

A RemoteEvent sends messages either way:

CallFromFires
FireServer(...)clientOnServerEvent(player, ...) on the server
FireClient(player, ...)serverOnClientEvent(...) on that client
FireAllClients(...)serverOnClientEvent(...) on every client

A RemoteFunction is a call the server answers. InvokeServer(...) yields on the client until the server's OnServerInvoke(player, ...) returns, and returns its values. An error in the handler is raised on the client. A call made before the handler is set waits for it, and a call still waiting when the client disconnects fails. OnServerInvoke can be set but not read. Servers can't call clients yet.

Remotes are matched by their path in the game, so give them the same name and parents on both sides. Putting them in the place does that for you. Arguments arrive as copies: numbers, strings, booleans, tables, and any datatype a place can save. An instance arrives as the instance at the same path on the other side (or nil), and players as their copies.

WebSockets ​

luau
local client = Http:CreateWebStreamClient(Enum.WebStreamClientType.WebSocket, { Url = "wss://echo.example.com" })
client.MessageReceived:Connect(function(message)
	print("got", message)
end)
client.Opened:Connect(function()
	client:Send(Http:JSONEncode({ hello = "world" }))
end)

Http:CreateWebStreamClient(Enum.WebStreamClientType.WebSocket, { Url = url, Headers = headers? }) opens a ws:// or wss:// connection on its own thread and returns the client at once. Needs the Http permission.

MemberDoes
Send(data)Sends text. Queued while connecting, dropped once closed
Close()Closes, then fires Closed
ConnectionStateConnecting, Open, Error or Closed
Opened(status, headers)Connected. Status is 101
MessageReceived(message)A whole message, text or binary
Closed()Either side closed
Error(status, message)Failed or dropped. status is the HTTP status if the server refused, else 0

Events fire between frames. Http keeps the client alive until it closes, so you don't have to hold on to it. SSE and RawStream clients aren't built yet, and the web build can't open WebSockets yet (it fires Error).

HTTP ​

Http:GetAsync, Http:DownloadAsync, JSONEncode and JSONDecode are in the Http API reference.

Apache License 2.0