Why the server is the only one who can give out coins, and what happens when you forget.
Here's the uncomfortable truth of online games: your players are not all trustworthy.
Some players modify the game running on their own computer. A few do it by hand, but the ones that matter do it automatically. These players are called exploiters, and building so they can't break anything is what this lesson is about.
Remember the two computers from the scripts lesson? A LocalScript runs on the player's machine. So this:
-- LocalScript (runs on the PLAYER's computer)
game.Players.LocalPlayer.leaderstats.Coins.Value = 999999
…runs on the player's own computer. And here's the horror: if the leaderstats folder is replicated to clients, that line can actually change what the leaderboard shows.
A player who wants free coins just runs one line of code. Congratulations — your economy is broken.
Where do LocalScripts run?
The rule, in one sentence:
The server is the only thing that can change real data. Clients can only request.
So coins work like this:
If an exploiter sends a million fake clicks, the server checks each one and says "no" — because the server doesn't care what the client's screen claims. The client can claim anything; the server awards what it verifies.
In a secure game, when the client clicks a coin, it sends a and the server does the
A server callback that rewards coins should always verify before it awards:
function onCoinClicked(player)
if player ~= nil and player:IsDescendantOf(game.Players) then
local coins = player.leaderstats.Coins
coins.Value = coins.Value + 10
end
end
The if checks the player is real before touching their data. That's the mindset: assume the worst, verify the minimum.
A player wants to buy something. Write the guard that only lets the purchase happen if their coins are at least 100.
-- guard the purchase
Your first games won't attract exploiters. But the habit matters: building "server decides" from day one costs nothing extra, and it means the day your game gets popular, you don't have to rebuild the economy.
An exploiter sends your server 1,000,000 fake 'click coin' requests. What happens if your reward code is server-side?
Next: the plumbing that lets the client ask the server — RemoteEvents.