Multiplayer, saving and debugging, lesson 28
Never trust the client
Goal: Spot and fix a remote that cheaters can abuse.
about 7 min3 practice exercises
A player's device is theirs. With cheating tools they can fire any RemoteEvent, as often as they like, with any values. Your LocalScripts are suggestions. Your server code is the law.
Here is a remote that looks fine and is a disaster:
-- Script (server) - do not do this
addCoins.OnServerEvent:Connect(function(player, amount)
player.leaderstats.Coins.Value += amount
end)
A cheater fires it with amount = 999999. The server obeys.
The fix is to change what the client is allowed to say. It never sends an outcome ("give me 25 coins"). It sends a request ("I want to collect this coin"), and the server decides:
-- Script (server)
local COIN_VALUE = 5
local lastCollect = {}
collectCoin.OnServerEvent:Connect(function(player, coin)
-- 1. Is the argument what we expect?
if typeof(coin) ~= "Instance" or not coin:IsDescendantOf(workspace.Coins) then
return
end
-- 2. Is the player asking too often?
local now = os.clock()
if now - (lastCollect[player] or 0) < 0.5 then
return
end
lastCollect[player] = now
-- 3. The server decides the amount.
player.leaderstats.Coins.Value += COIN_VALUE
end)
Three habits cover most cases:
- Check every argument. Right type, a real thing, a sensible value.
- Limit how often. Remember the last request per player.
- Never accept amounts, prices or results from the client. Look them up on the server.
A full version would also check the player is close enough to the coin. The Rodex guide on backdoors linked below covers the related problem of other people's code.
Go deeper on Rodex
Sources
Checked against Roblox's documentation on . The lesson is in our own words; these pages are the source of truth.