Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท Multiplayer and Security

Remote Events: Talking Between Client and Server

Learn how a RemoteEvent sends messages from a client to the server and back with FireServer, OnServerEvent, FireClient and FireAllClients, with safe examples.

The client and the server live on different computers, so they cannot call each other's functions directly, and a BindableEvent cannot cross between them. When the two sides must talk, Roblox gives you the RemoteEvent: a one-way message that travels across the network.

A RemoteEvent is an instance, usually stored in ReplicatedStorage so both sides can find it. It has methods for sending and events for receiving. The Roblox code below is read-only; the runnable examples are a JavaScript model of a network with a server and clients.

The four directions

Direction Sender Receiver
Client to server remote:FireServer(...) remote.OnServerEvent:Connect(function(player, ...))
Server to one client remote:FireClient(player, ...) remote.OnClientEvent:Connect(function(...))
Server to everyone remote:FireAllClients(...) remote.OnClientEvent:Connect(function(...))

Two details matter right away:

  1. When a client fires, the server's handler automatically receives the player who sent it as the first argument. The client cannot fake it, so you always know who is asking.
  2. Arguments after that are whatever the client chose to send, and they are untrusted. The server must always check them.

A complete example: a "pickup" request

Setup in Studio: create a RemoteEvent named CollectCoin in ReplicatedStorage.

Client side (a LocalScript, for example in StarterPlayerScripts):

LUA
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local collectCoin = ReplicatedStorage:WaitForChild("CollectCoin")

-- Ask the server to give us the coin
collectCoin:FireServer(coin)

Server side (a Script in ServerScriptService):

LUA
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local collectCoin = ReplicatedStorage.CollectCoin

collectCoin.OnServerEvent:Connect(function(player, coin)
	-- 1. Validate: is it really a coin that still exists?
	if typeof(coin) ~= "Instance" or not coin:IsDescendantOf(workspace.Coins) then
		return
	end

	-- 2. Validate: is the player close enough?
	local character = player.Character
	local root = character and character:FindFirstChild("HumanoidRootPart")
	if not root or (root.Position - coin.Position).Magnitude > 15 then
		return
	end

	-- 3. Act
	coin:Destroy()
	player.leaderstats.Coins.Value += 1
end)

The server never takes the client's word for it. It checks the argument type, that the coin is in the right folder, and that the player is nearby. Only then does it change the game. This validate then act pattern is the heart of safe multiplayer code and is explored fully in anti-exploit security.

A safe remote handler

Receive The player arrives as argument one.
Validate Check types, ranges and ownership.
Act Only the server changes the game.
Tell clients Fire back for effects and UI.

Never trust any argument after the player.

1 / 2

Server to client: sending feedback

After the server acts, clients usually need to hear about it, to play a sound, show a message or update a custom interface.

LUA
-- Server
local notify = ReplicatedStorage.Notify
notify:FireClient(player, "You collected a coin!")          -- one player
notify:FireAllClients(player.Name .. " found a rare coin!") -- everybody
LUA
-- Client
notify.OnClientEvent:Connect(function(message)
	messageLabel.Text = message
end)

The client cannot "refuse" a message from the server; it only decides how to display it.

What can be sent

Arguments are serialized and copied. Numbers, strings, booleans, Vector3, CFrame, Color3, instances that exist on both sides, and tables of these all work. Things that do not survive: functions, metatables on tables, instances that exist only on one side (like a part created by a LocalScript), and tables with mixed or non-string keys. Large tables cost bandwidth, so send small messages and IDs rather than whole objects.

Also, remotes are asynchronous: FireServer returns immediately and does not wait for an answer. If you need an answer, use a RemoteFunction (next lesson), or have the server fire back a second event.

Try the network model

Here is a small JavaScript network with FireServer, FireClient and FireAllClients. The server handler receives the sender automatically, and we make a hostile client lie in the arguments.

JS RemoteEvent model (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Only Ana's first request changed the state. Ben's request for the same coin and the two malformed requests were rejected, and the sender names were provided by the network, not by the clients.

Dealing with many events

Some beginners create a RemoteEvent for every tiny action. A cleaner design uses a handful of remotes with a short action name as first argument, or one folder of remotes grouped by feature (Shop, Combat, Round). Whichever you choose, give each remote a precise name and handle it in one place on the server.

JS One remote, several actions (dispatch table)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

A dispatch table keeps the server in control: only known action names run, and unknown ones are dropped.

Common mistakes

  • Forgetting that the first server argument is the player. The handler is function(player, ...), not function(...).
  • Trusting client-sent values. A client can send -9999 as a price or someone else's name.
  • Firing from the wrong side. FireServer is client only; FireClient and FireAllClients are server only.
  • Firing before the listener exists. Connect handlers early, and use WaitForChild on the remote.
  • Spamming. A client can fire a remote hundreds of times a second. Add rate limits (covered in the next lessons).
  • Sending instances that exist only locally. The server cannot see them and receives nil.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the server's OnServerEvent handler receive first?

    Roblox adds the sending player as the first argument, and the client cannot fake it.
  2. 2. Which call sends a message from the server to every client?

    FireAllClients broadcasts to all clients. FireClient targets one player and FireServer goes the other way.
  3. 3. Why should the server check the arguments of OnServerEvent?

    Clients can be modified, so all arguments are untrusted input and must be validated.
  4. 4. Where is a RemoteEvent normally stored so both sides can use it?

    Both server and client can see ReplicatedStorage. ServerStorage is hidden from clients.

Summary

  • A RemoteEvent is a one-way network message between client and server, stored in ReplicatedStorage.
  • Client to server: FireServer and OnServerEvent (player first). Server to client: FireClient, FireAllClients and OnClientEvent.
  • Treat every client-sent argument as untrusted: check type, range and ownership before acting.
  • Send small data, group actions sensibly and use a dispatch table for multi-action remotes.
  • Remotes do not return values; for questions and answers, use a RemoteFunction.

Next: RemoteFunctions and Input Validation