Part 7 mini-project: Touch-to-Collect Coin
The last project of the book is a real Roblox Studio scene: a coin that gives the player a point when touched, then disappears. It uses tables, methods, events, and one of the Roblox-specific globals from chapter 31.
This is the first thing in the book that runs inside Roblox Studio, not the terminal. If you have not installed Studio, go back to the end of chapter 31 and do that first.
What to build
When the experience starts:
- Each player gets a small leaderstats folder with a
value called Coins, starting at
0. The leaderboard at the top of the screen shows this automatically. - A yellow coin (a small Part) sits in the world.
- When the player walks into the coin:
- Their
Coinsvalue goes up by1. - The coin disappears.
- Their
Files
Two short script files are in
projects/06-touch-coin/:
player-setup.lua— goes inside aScriptin ServerScriptService. It createsleaderstatsandCoinsfor each joining player.coin-touched.lua— goes inside aScriptunder the Coin Part. It handles the touch.
That folder also has a README.md reminding you of the
Studio steps below.
Studio setup, step by step
- Open Studio. Use the Baseplate template. A flat grey plate appears.
- Insert the Coin Part.
- In the Explorer panel (top right), right-click Workspace and choose Insert Object -> Part.
- Rename the new Part to
Coinby selecting it and pressing F2, or by double-clicking its name in Explorer. - In the Properties panel, set:
Anchored=true(so it does not fall)Shape=Cylinder(optional; makes it look like a coin)Size=2, 0.4, 2BrickColor=Bright yellowPosition= roughly0, 5, 0(or wherever the player can reach it from the spawn point)
- Add a Script to the Coin.
- Right-click the Coin in Explorer -> Insert Object -> Script.
- The Script opens in the editor. Delete the example code and paste
the contents of
projects/06-touch-coin/coin-touched.lua.
- Add the player-setup Script.
- In Explorer, find ServerScriptService. Right-click it -> Insert Object -> Script.
- Rename it
PlayerSetup. - Paste the contents of
projects/06-touch-coin/player-setup.lua.
- Test it.
- Click the Play button at the top.
- A test character appears. Walk into the coin.
- The leaderboard at the top right should show Coins: 1.
- The coin disappears.
- Stop with the Stop button (or close the test window).
How the code maps to chapters
coin-touched.luauses an event handler —:ConnectonTouched— just like chapter 31. Finding a Player from a touching Part is two lines: a function call and anif.player-setup.luausesInstance.new(chapter 31), property assignment (folder.Name = "leaderstats"), and thePlayerAddedevent. It builds a small object tree inside each player.
Underneath it is standard Lua: if, function calls, dot
lookups, colon-syntax methods. The Roblox-specific bits are the names
(game, Players, Instance) and the
wiring (events). Everything else is like the rest of the book.
A bigger challenge (optional)
If the basic version works, try one of these:
- Multiple coins. Right-click the Coin in Explorer, choose Duplicate, and move the copy elsewhere. Each Coin has its own Script, so it just works.
- Respawn. Instead of destroying the Coin, hide it
(
coin.Transparency = 1,coin.CanTouch = false) for five seconds, then bring it back (coin.Transparency = 0,coin.CanTouch = true). Usetask.wait(5)inside the touch handler. - A goal. When a player reaches 5 coins, show a chat
message like
<name> reached 5 coins!withgame.StarterGui:SetCore("ChatMakeSystemMessage", { Text = ... }).
None of these are required to finish the project.
Done?
When the Play test shows the coin disappearing and the leaderboard going up by 1, the project is complete. The very last project — Collect-All-Coins — grows this into a small game with several coins and a win condition.