Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 37 ยท Projects and Practice

Project: Coin Collector With a Shop and Saving

Combine spawning coins, server-validated pickups, a shop UI with RemoteFunctions and saved inventory into one complete Roblox project, with runnable logic models for each system.

This second project ties the whole course together. Players run around collecting coins that appear in the world. They open a shop menu, spend coins on upgrades, and their coins and purchases are saved between visits. You will use services, events, remotes, validation and DataStores, and you will see how the pieces fit into a clean architecture.

Roblox code is shown read-only. Every system has a runnable JavaScript model so you can verify the rules first.

Architecture

Plan the pieces before writing code. Each has one job and lives where it belongs.

Who does what

Coin spawner Server. Spawns and removes coins.
Shop service Server. Prices, buying, inventory.
Data module Server. Load and save.
Shop UI Client. Shows items, asks to buy.

The client asks, the server decides.

1 / 2
Piece Type and location
Data ModuleScript in ServerScriptService: load, save, session table
CoinSpawner Script in ServerScriptService
ShopService Script in ServerScriptService, owns the BuyItem remote
Config ModuleScript in ServerStorage with item prices and effects
ShopCatalog ModuleScript in ReplicatedStorage with names and prices for display only
ShopUI LocalScript in StarterGui
BuyItem RemoteFunction in ReplicatedStorage

The catalog the client sees is only for showing; the real prices live on the server, as you learned in validation.

System 1: coins

Spawn a coin at a random point of a spawn area, remove it when collected, and keep a maximum number in the world.

LUA
-- CoinSpawner (Script)
local Players = game:GetService("Players")
local coinTemplate = game:GetService("ServerStorage").Coin
local area = workspace.CoinArea -- an anchored, transparent, non-colliding Part
local MAX_COINS = 20

local function randomPoint()
	local half = area.Size / 2
	local offset = Vector3.new(
		(math.random() - 0.5) * area.Size.X,
		half.Y + 2,
		(math.random() - 0.5) * area.Size.Z
	)
	return area.Position + offset
end

local function spawnCoin()
	local coin = coinTemplate:Clone()
	coin.Position = randomPoint()
	coin.Parent = workspace.Coins
	local collected = false
	coin.Touched:Connect(function(hit)
		if collected then return end
		local player = Players:GetPlayerFromCharacter(hit.Parent)
		if not player then return end
		collected = true -- debounce
		coin:Destroy()
		Data.addCoins(player, 1)
	end)
end

while true do
	if #workspace.Coins:GetChildren() < MAX_COINS then
		spawnCoin()
	end
	task.wait(1)
end

The collected flag is a debounce, so two players touching at once cannot both get paid. Because the server handles the touch, nobody can award themselves coins from a client.

JS Coin pool: spawn, collect, cap (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

System 2: the shop

The shop has a config of items, an inventory per player, and one function that handles a purchase. Keep this logic in a plain function so it is easy to test.

LUA
-- Config (ModuleScript in ServerStorage)
return {
	SpeedBoots = { price = 50, maxOwned = 1 },
	CoinMagnet = { price = 120, maxOwned = 1 },
	Hat = { price = 20, maxOwned = 5 },
}
LUA
-- ShopService (Script)
local buyItem = ReplicatedStorage.BuyItem
local Config = require(ServerStorage.Config)
local lastCall = {}

buyItem.OnServerInvoke = function(player, itemName)
	if typeof(itemName) ~= "string" then return false, "bad request" end
	local now = os.clock()
	if now - (lastCall[player] or 0) < 0.5 then return false, "slow down" end
	lastCall[player] = now

	local item = Config[itemName]
	if not item then return false, "unknown item" end

	local data = Data.get(player)
	if not data then return false, "data not loaded" end
	if (data.inventory[itemName] or 0) >= item.maxOwned then return false, "already owned" end
	if data.coins < item.price then return false, "not enough coins" end

	data.coins -= item.price
	data.inventory[itemName] = (data.inventory[itemName] or 0) + 1
	Data.syncLeaderstats(player)
	return true, "purchased"
end

Compare this to Remote Functions and Input Validation: the client sends only a name; the server checks the type, rate, item, ownership limit and funds.

JS Shop purchase logic with results (JavaScript stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the order of the log: the second SpeedBoots fails with "already owned", CoinMagnet fails (not enough coins after the boots), and cheap hats go through until the budget runs out.

System 3: saving

Reuse the structure from Saving Player Data: load into a session table with retries, expose Data.get(player), Data.addCoins, and save on leave and shutdown. Your saved table might look like this:

LUA
local DEFAULTS = {
	version = 1,
	coins = 0,
	inventory = {}, -- [itemName] = count
}

Remember the two rules: never save if loading failed, and keep the session table the only place the game changes during play.

System 4: the shop UI

The client builds the shop menu from ShopCatalog, shows coins from the leaderstats value, and calls the RemoteFunction.

LUA
-- ShopUI (LocalScript)
local buyItem = ReplicatedStorage:WaitForChild("BuyItem")

local function onBuyClicked(itemName, button)
	button.Active = false -- prevent double clicks while waiting
	local ok, message = buyItem:InvokeServer(itemName)
	statusLabel.Text = ok and ("Bought " .. itemName) or ("Could not buy: " .. message)
	button.Active = true
end

Use the UIListLayout and UDim2 ideas from UI layout, and give feedback with a short tween. The UI is a convenience only; if an exploiter bypasses it, the server still enforces every rule.

Extension ideas

  • A CoinMagnet item that the server applies by pulling nearby coins toward the player.
  • A leaderboard with an OrderedDataStore, from DataStore limits.
  • A developer product that sells a coin pack, processed with ProcessReceipt from monetization.
  • Rare golden coins worth 10, with weighted random spawning.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should the real item prices live?

    Prices must be on the server so exploiters cannot change them. The client only shows display copies.
  2. 2. Why does the coin script set collected = true or delete the coin first?

    It is a debounce that makes collection happen once.
  3. 3. What does the client send when buying an item?

    The server looks up price and ownership itself, so sending anything more would just be untrusted data.
  4. 4. Which system is responsible for saving coins and inventory?

    Only the server can use DataStores, and it keeps the authoritative session data.

Summary

  • Split the game into small systems: coin spawner, shop service, data module, config and UI.
  • Keep prices, inventory and coins on the server; the client only shows and asks.
  • Debounce pickups, validate shop requests, rate limit, and return clear messages.
  • Reuse the load, session and save pattern for persistent coins and items.
  • Extend the project with magnets, leaderboards, products and rare coins.

Next: Practice Challenges and Solutions