OnServerEvent can only be used on the serverin Roblox Luau: what it means and how to fix it

What it means

A LocalScript tried to listen to OnServerEvent. That event only fires on the server, when a player's device calls FireServer.

The smallest script that triggers it

Triggers the errorline 5: error here
-- LocalScript in StarterPlayerScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage:WaitForChild("ShowMessage")
 
remote.OnServerEvent:Connect(function(text)
  print(text)
end)

The fix

Fixedline 5: the fix
-- LocalScript in StarterPlayerScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remote = ReplicatedStorage:WaitForChild("ShowMessage")
 
remote.OnClientEvent:Connect(function(text)
  print(text)
end)

A LocalScript receives messages from the server through OnClientEvent. OnServerEvent belongs in a server Script.

Common causes

  1. The listener is in a LocalScript and should use OnClientEvent.
  2. The code was meant for a server Script and was placed in a LocalScript.
  3. Client and server halves of a RemoteEvent were written the wrong way round.

Practice this until it sticks

A Deep Dive on RemoteEvents, client and server: 5-minute lessons, each with a quiz. What you miss comes back in the next module. One Deep Dive a day is free. No card.