Home / Lessons / Lesson 11
Intermediate Free

RemoteEvents

If a LocalScript clicks a button to buy a sword, the Server won't know about it. RemoteEvents allow the Client and Server to communicate.


What is a RemoteEvent?

A RemoteEvent is a special object (usually placed in ReplicatedStorage) that acts like a telephone wire. A LocalScript can send a message across the wire, and a Script on the server can listen for that message and execute code.

Client to Server (FireServer)

Imagine a player clicks a "Spawn Car" button on their screen. The UI is handled by a LocalScript. It uses :FireServer() to send a request.

LocalScript (Client)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCarEvent")

-- When the button is clicked
button.MouseButton1Click:Connect(function()
    -- Ask the server to spawn a "SportsCar"
    SpawnCarEvent:FireServer("SportsCar")
end)

Server Listening (OnServerEvent)

On the server, a Script listens for that specific RemoteEvent. When it hears the signal, it actually spawns the car.

Script (Server)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCarEvent")

-- The first parameter is ALWAYS the player who fired it!
SpawnCarEvent.OnServerEvent:Connect(function(player, carType)
    -- Sanity Check: Ensure the requested car is a string
    if type(carType) ~= "string" then return end
    
    -- Spawn the car for the player
    print(player.Name .. " spawned a " .. carType)
end)

The Golden Rule of Remotes

Never Trust the Client. If an exploiter fires the SpawnCarEvent 5,000 times per second, your server will crash. If they pass a table instead of a string for carType, your server will error. You must implement cooldowns, cost checks, and type-checks directly inside the OnServerEvent connection before executing the code.