Coins: touch, debounce, respawn, leaderstats

Make a Roblox coin pickup that cannot be farmed. Follow Beacon Yard's CoinService through Touched, a per-player debounce, a distance check, a respawn timer and the leaderstats leaderboard.

Project lesson, about 70 minutes, 10 steps

By the end you can

  • Trace one coin from Touched through the server checks to the leaderstats IntValue
  • Explain why one visit pays once, and why a far touch must not burn the pickup
  • Change COIN_VALUE and predict which tests move

Before this: World building in code: MapBuilder, lighting, atmosphere, Security: never trust the client

Step 1 of 10

One step, a dozen touches

Back at m02-the-pad, the yard started with one pad and a bare script that printed every time something touched it. Walk onto that pad once and Output fills up: LeftFoot touched Pad1, LeftLowerLeg touched Pad1, RightFoot touched Pad1, and again as you shuffle.

pad.Touched:Connect(function(hit: BasePart)
	print(hit.Name .. " touched " .. pad.Name)
end)

Touched fires once for every part that starts touching, not once per player. If each touch paid 5 coins, one step would pay 50.

Touched is a bump sensor. It tells you that something bumped the part, and nothing else: not who, not how many times they meant to, not whether it was fair.

Think of it as A bump sensor

Server sideRemember: The Touched event is a bump sensor.

Choose one

A player walks onto the m02 pad and stops. Why does Output show several lines?

A player walks onto the m02 pad and stops. Why does Output show several lines?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 2 of 10

CoinService pays on the server

m03-coins-count replaces the print with CoinService. It spawns a coin above each pad and pays from the coin's own Touched handler, on the server. A client cannot give itself coins, because the client never runs this code.

Walking onto a pad: the server pays 5 and destroys the coin.

Transcript
  1. 0:00 Walking onto the pad fires Touched on the server.
  2. 0:03 The root is within 12 studs, so the server adds 5 coins.
  3. 0:07 The leaderboard IntValue updates from the profile. The coin part is destroyed.

The handler finds the player from the part that touched, using GetPlayerFromCharacter. A touch from a crate or a falling part has no player, and the handler returns early.

	local claimed = false
	local seen: { [Player]: boolean } = {}
 
	coin.Touched:Connect(function(hit: BasePart)
		if claimed then
			return
		end
		local player = playerFromHit(hit)
		if not player then
			return
		end
		if seen[player] then
			return
		end
		seen[player] = true
		claimed = true
 
		local value = coin:GetAttribute("CoinValue")
		local amount = if typeof(value) == "number" then value else Config.COIN_VALUE
		DataService.addCoins(player, amount)
		coin:Destroy()
Work it out

Config.COIN_VALUE is 5. A player collects three coins from three different pads. How many coins does leaderstats show if they started at 0?

coins

Link to this step · This confused me

Step 3 of 10

Two flags make one payout

The coin keeps two guards. claimed turns true the moment anyone is paid, so later touches return at once. seen is a table keyed by player, so the same player cannot be paid twice by this coin. Guards like these are called a DebounceDebounceA guard that turns many rapid triggers into one action.Think of it as an elevator call button that ignores mashing..

A debounce is the elevator call button. Press it ten times and the elevator still comes once. The building remembers the first press and ignores the rest until the trip is over.

Think of it as An elevator button that ignores mashing

Server sideRemember: A debounce is an elevator button that ignores mashing.

Luau
Ctrl Enter runs the cell. Checked against the expected output
Choose one

In the cell above, Ben touches the coin right after Ada. Why does Ben get nothing?

In the cell above, Ben touches the coin right after Ada. Why does Ben get nothing?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 4 of 10

A touch is only a hint

At m04-two-sides the handler gained one more check. Before paying, the server measures the distance from the character's HumanoidRootPart to the coin. More than 12 studs away, it returns. That stops a forged touch from across the yard. It does not stop a cheater who teleports their own character onto the coin, because the client owns its character's position; Security: never trust the client covers why.

		-- A touch is only a hint. Check the character's root is close
		-- enough to the coin before paying.
		local character = player.Character
		local rootPart = if character then character:FindFirstChild("HumanoidRootPart") else nil
		if rootPart == nil or not rootPart:IsA("BasePart") then
			return
		end
		if (rootPart.Position - coin.Position).Magnitude > 12 then
			return
		end
		seen[player] = true
		claimed = true

Look at the order. The distance check comes before seen[player] = true. If it came after, a forged far touch would mark the player as seen and lock them out of a coin they never collected.

Find the broken line

This handler locks honest players out after a bad touch. Which line is in the wrong place?

This handler locks honest players out after a bad touch. Which line is in the wrong place?

Press 1 to 5 to choose, Enter to check.

Link to this step · This confused me

Step 5 of 10

leaderstats is a display copy

The number players see in the top-right list comes from a folder named exactly leaderstats under the Player, holding an IntValue named Coins. Roblox shows any value in that folder on the session leaderboard. The name must be all lowercase, or the player is not added to the board1.

local function ensureLeaderstats(player: Player, profile: Profile)
	local existing = player:FindFirstChild("leaderstats")
	if existing then
		existing:Destroy()
	end
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = profile.coins
	coins.Parent = leaderstats
	leaderstats.Parent = player
	coinStats[player] = coins

The real balance is profile.coins inside DataService, on the server. addCoins changes the profile, then copies the number into the IntValue. The HUD reads the IntValue too. If a client changes its own copy of the IntValue, only that screen changes.

Roblox Studio
Beacon Yard during Play: the HUD at the top left and the player list at the top right show the same coin total.

Select a numbered pin, or a row below, to read what it marks.

The HUD and the player list read the same IntValue.

Predict the output

DataService names the folder Leaderstats with a capital L. What do players see?

local leaderstats = Instance.new("Folder")
leaderstats.Name = "Leaderstats"
leaderstats.Parent = player
DataService names the folder Leaderstats with a capital L. What do players see?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 6 of 10

The server respawns the coin

After paying, the handler destroys the coin and calls task.delay with Config.COIN_RESPAWN_SECONDS, which is 4. The new coin is a new part with a new seen table, so the pad pays again. The tests shorten the wait with a workspace attribute, TestCoinRespawnSeconds. The game itself never sets it.

Standing on the pad: the coin returns after about 4 seconds and pays again.

Transcript
  1. 0:02 The first coin is gone. Standing here does not pay again.
  2. 0:04 After 4 seconds the server spawns a new coin.
  3. 0:09 The new coin has its own debounce table, so the second touch pays another 5.
Put in order

Put one pickup in the order the server runs it.

  1. Wait COIN_RESPAWN_SECONDS, then spawn a new coin
  2. Find the player and check the 12-stud distance
  3. Set seen and claimed, then add CoinValue to the profile
  4. Touched fires on the coin
  5. Destroy the coin

Link to this step · This confused me

Step 7 of 10

Where the elevator picture breaks

Where does it break?“A bump sensor” is a good picture of the touched event. Where does it stop being true?

It breaks here. It fires many times for one contact, and a part moved by setting its CFrame (PivotTo) makes no bump by moving, so a drone that lands on a player standing still never fires it.

The true model. Touched fires when the physics engine sees two parts start touching through simulated movement, and at least one of them must be unanchored. It fires per part, repeatedly, and not when a script sets a CFrame so that parts overlap. Gameplay needs its own guard, and moving hazards often use a distance check instead.

The bump sensor breaks the same way: one contact can bump many times, and an anchored part moved by setting its CFrame makes no bump by moving. The elevator button works for everyone in the building. A game debounce cannot. Answer first, then open the breakdown.

Where does it break?“An elevator button that ignores mashing” is a good picture of a debounce. Where does it stop being true?

It breaks here. One button serves everyone, but a game debounce must be kept per player and on the server, or one player blocks another and an exploiter skips it.

The true model. A debounce is state the server keeps, usually per player, that says an action already happened. Checks that decide whether the action is legal run before that state is written.

Choose all that apply

Which of these are true for Beacon Yard's coin debounce?

Which of these are true for Beacon Yard's coin debounce?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 8 of 10

Build it: inspect and test CoinService at m06

Other ways pros do this

OptionWhat it isWhen pros pick itTrade-offs
Touched on the coinThe engine reports contact; the server checks it.Pickups, pads and triggers the player walks into. What Beacon Yard uses.Fires many times and can be faked by a client that owns its character, so it needs a debounce and a server distance check.
Spatial queryworkspace:GetPartsInPart or GetPartBoundsInBox, run on a timer or on Heartbeat.Zones that must know who is inside right now, such as capture points.You pay for every query, even when nobody is near.
ProximityPromptA key or tap the player presses near a part.Deliberate actions, such as the beacon's Start drill.Slower than walking over a coin, and not suited to many small pickups.
  • src/server/CoinService.luau
  • src/server/DataService.luau
  • src/shared/Config.luau
  • src/server/init.server.luau
  1. In the game repo, run git diff m02-the-pad m04-two-sides -- src/server/CoinService.luau src/server/DataService.luau. (Later milestones only dress the coin in an uploaded mesh; the rules stay the same.)

    You should see The diff adds CoinService.luau with the claimed and seen debounce, the 12-stud distance check, the respawn timer and DataService.addCoins, plus the leaderstats code in DataService.luau.

  2. Check out m06-drone-wrench and open src/shared/Config.luau and src/server/CoinService.luau.

    You should see Config.luau holds COIN_VALUE = 5 and COIN_RESPAWN_SECONDS = 4. CoinService.luau keeps claimed, seen, the 12-stud check and DataService.addCoins.

  3. Run rojo serve, connect Studio and press Play. Walk onto a pad.

    You should see A coin floats above each of the six pads. Walking onto one adds 5 to Coins on the player list, and a new coin appears about 4 seconds later.

  4. From the server command bar, run game.ServerScriptService.RunGameTests:Invoke().

    You should see Output prints TEST PASS coin_pickup and TEST PASS coin_respawn.

Link to this step · This confused me

Step 9 of 10

Challenges

Level 1, Apprentice

Challenge

Make each coin worth 10, see which test notices, then put it back.

Start from
The game at milestone m06 (download)

Done means

  • A pickup shows +10 during Play
  • config_numbers fails with Config.luau drifted from the contract
  • COIN_VALUE is 5 again afterwards

Meet the criteria above, then record your own result. No test grades this check.

Choose one

You set COIN_VALUE to 10. Which test fails?

You set COIN_VALUE to 10. Which test fails?

Press 1 to 4 to choose, Enter to check.

Level 2, Builder

Challenge

Shrink the reach to 2 studs and feel the difference, then restore 12.

Start from
The game at milestone m06 (download)

Done means

  • With the limit at 2 studs, stopping just short of a pad does not pay
  • Standing on the pad still pays
  • The limit is 12 again afterwards

Meet the criteria above, then record your own result. No test grades this check.

Level 3, Professional

Challenge

Add one bonus coin worth 15 with its own tag and its own respawn timer, without changing the six pad coins.

Start from
The game at milestone m06 (download)
Grade it
To grade work from an earlier milestone, apply your challenge changes to a checkout based on m14-ship, which includes the Course Runner. In Studio, press Play, then run game.ServerScriptService.RunGameTests:Invoke("coins", "coins-bonus") in the server command bar. Copy the pass code from Output.

Done means

  • A part tagged BonusCoin, not Coin, with CoinValue 15, parented to Workspace.Yard
  • Collecting it pays 15 through DataService.addCoins with the same 12-stud rule
  • It respawns after 8 seconds, or TestBonusRespawnSeconds when that is set
  • The test prints TEST PASS bonus_tagged, bonus_pay, bonus_respawn and pad_respawn_separate

The Course Runner prints this code when the test passes.

Link to this step · This confused me

Step 10 of 10

Recap

Retiring the analogy

You no longer need “an elevator button that ignores mashing”. A debounce is state the server keeps, usually per player, that says an action already happened. Checks that decide whether the action is legal run before that state is written.

Recap

  1. Touched is a bump sensor: it fires per body part, so the server turns many bumps into one decision.
  2. A debounce that matters lives on the server and is kept per player; legality checks come before the flags are set.
  3. leaderstats is a display copy named in lowercase; DataService's profile is the real balance.

The Touched event is a bump sensor, until it fires many times for one contact, and a part moved by setting its CFrame (PivotTo) makes no bump by moving, so a drone that lands on a player standing still never fires it.

A debounce is an elevator button that ignores mashing, until one button serves everyone, but a game debounce must be kept per player and on the server, or one player blocks another and an exploiter skips it.

Next The round loop: a state machine

Link to this step · This confused me

Sources
  1. Leaderboards. Roblox Creator Documentation, checked 2026-11-13.