Capstone, part 3: multi-product receipts and party matchmaking

Part 3 of the Beacon Yard capstone replaces basic single-product and solo-queue drills with two harder systems from scratch: a multi-product receipt router with rollback and an atomic party matchmaker.

Project lesson, about 70 minutes, 5 steps

By the end you can

  • Design and pass a multi-product ProcessReceipt router that handles catalog lookup, replay SKU deduplication, and rollback on save or receipt-write failure
  • Design and pass an atomic party matchmaking planner that never splits friend groups, handles oversized parties without head-of-line blocking, and respects wait timeouts
  • Grade both systems in the browser Luau runner or in Studio with challenges/capstone_catalog.luau and challenges/capstone_party_queue.luau

Before this: Capstone, part 2: cosmetic pass trail, localization and thumb targets, MemoryStore and MessagingService: matchmaking across servers, Monetization: products, passes, subscriptions, economy

Step 1 of 5

Project 5: multi-product receipts with two-step failure rollback

The Coin Pack handler had a gap before m14-ship. In m12-money and m13-art-pass it saved the profile and then wrote the purchase id as a separate r_<purchaseId> row. A server that stopped between the two writes left the coins saved and no record, so the redelivered receipt paid again. That crash window is what the drill below makes you reason about.

Stored dataRemember: ProcessReceipt is a courier who keeps coming back until you sign.

At m14-ship the id lives in the profile itself (recentPurchaseIds, the last 50). ReceiptRules.applyReceipt adds the coins and the id in one step, a repeat delivery finds the id and grants nothing, and undoReceipt takes the grant back when the save fails. Look at how MonetizationService.luau handles it1.

	local applied = ReceiptRules.applyReceipt(data, receipt, Config.COIN_PACK_COINS)
	if applied == nil then
		-- The id appeared in the profile while the old row was being read.
		return granted
	end
	DataService.syncCoins(player)
 
	if not DataService.save(player) then
		ReceiptRules.undoReceipt(data, receipt, Config.COIN_PACK_COINS, applied)
		DataService.syncCoins(player)
		return notYet

The safe order from Roblox's guide to atomic purchase handling2 keeps the receipt id inside the player's own saved data, the same order as the monetization lesson:

  1. Check the purchase id is not already recorded in the player's data. If it is, and that record was saved, return PurchaseGranted.
  2. Add the coins to the player's data in memory.
  3. Write the purchase id into that same player data.
  4. Save that one record and wait for the save to land.
  5. Return PurchaseGranted only if the save worked. Otherwise return NotProcessedYet.

One write carries both facts, so a crash leaves both or neither. The drill below keeps two flags, saveOk and recordOk, only so the test can fail each write on purpose. It shows the logic's shape, not that two writes are safe.

Now extend that single-product handler to a product catalog ({ [productId] = { coins = ..., sku = ... } }) that returns three values: (newBalance, decision, analyticsSku). Notice the extra rule for replays: when seen[purchaseId] is already true, return (balance, "PurchaseGranted", nil) so Roblox stops redelivering AND your economy funnel does not log the same purchase twice.

Choose one

A player buys BigPack (150 coins, sku 'BigPack'). The first call succeeds and marks seen['p-9'] = true. Roblox delivers 'p-9' a second time. What must ReceiptCatalog.process return?

A player buys BigPack (150 coins, sku 'BigPack'). The first call succeeds and marks seen['p-9'] = true. Roblox delivers 'p-9' a second time. What must ReceiptCatalog.process return?

Press 1 to 4 to choose, Enter to check.

Choose one

A test receipt arrives with productId = 0. What should ReceiptCatalog.process return?

A test receipt arrives with productId = 0. What should ReceiptCatalog.process return?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 2 of 5

Build and grade the multi-product receipt router

Complete process in the browser cell below. To grade it in Studio, copy your code into challenges/capstone_catalog_work.luau, press Play and run game.ServerScriptService.RunGameTests:Invoke("capstone_catalog", "capstone-catalog") in the server command bar.

Luau
Ctrl Enter runs the cell. Checked by tests

Level 3, Professional

Challenge

Pass the browser cell above (and optionally challenges/capstone_catalog.luau in Studio). Solution: challenges/solutions/capstone-systems.md.

Start from
The game at milestone m14 (download)
Check it here
Run the code cell; the challenge completes when it passes. No Studio needed.
Or grade it in Studio
In Studio, press Play, then run game.ServerScriptService.RunGameTests:Invoke("capstone_catalog", "capstone-catalog") in the server command bar. Copy the pass code from Output.

Done means

  • Product id 0 or any id missing from catalog returns (balance, NotProcessedYet, nil) without touching seen
  • A first valid purchase with saveOk == true and recordOk == true grants entry.coins, marks seen[purchaseId], and returns PurchaseGranted with entry.sku
  • A replay of the same purchaseId adds 0 coins and returns PurchaseGranted with nil SKU
  • When saveOk == false or recordOk == false, balance and seen stay unchanged and NotProcessedYet is returned so a later retry pays cleanly
Have a Studio pass code?

The Course Runner prints this code when the test passes.

Where does it break?“A courier who keeps coming back until you sign” is a good picture of processreceipt. Where does it stop being true?

It breaks here. You decide what a duplicate looks like: without a stored purchase id, a redelivered receipt grants twice, and a record saved apart from the coins leaves a gap.

The true model. ProcessReceipt is one callback, set once from a server script (BindReceiptHandler is the newer route). Record the purchase id in the same save as the grant, then return PurchaseGranted. NotProcessedYet leaves the receipt unresolved: it returns on a rejoin or the next purchase, not on a timer.

Choose one

Why must a failed receipt write (saveOk == true, recordOk == false) roll back the coins before returning NotProcessedYet?

Why must a failed receipt write (saveOk == true, recordOk == false) roll back the coins before returning NotProcessedYet?

Press 1 to 4 to choose, Enter to check.

Choose one

The server crashes after the profile save succeeded but before the receipt record was written. Roblox delivers the same receipt again. Which design pays the player only once?

The server crashes after the profile save succeeded but before the receipt record was written. Roblox delivers the same receipt again. Which design pays the player only once?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 3 of 5

Project 6: atomic party matchmaking from scratch

In m11-two-places, MatchRules.plan groups individual QueueItems into matches of size, launching early when the oldest player has waited waitSeconds.

function MatchRules.plan(items: { QueueItem }, now: number, size: number, waitSeconds: number): { number }?
	local group: { number } = {}
	local seen: { [number]: boolean } = {}
	local oldest = math.huge
	for _, item in items do
		if not seen[item.userId] and #group < size then
			seen[item.userId] = true
			table.insert(group, item.userId)
			oldest = math.min(oldest, item.at)
		end
	end
	if #group == 0 then
		return nil

Real multiplayer games let friends join as parties. A party { members = { 1, 2, 3 }, at = 100 } is atomic: you can never send player 1 to match A and players 2, 3 to match B. Write planMatches(parties, now, maxSize, minPlayers, waitSeconds) with five rules:

  1. Deduplicate userIds across all parties in join order (first appearance wins); drop any party left empty. Never mutate the caller's input tables.
  2. Never split a party across two matches.
  3. A party larger than maxSize cannot fit in any match; leave it in waiting without blocking smaller parties behind it.
  4. Build matches greedily in join order: start a match with the earliest unused party that fits, then scan later unused parties in join order to fill remaining seats up to maxSize.
  5. Emit a match when #players == maxSize, or when #players >= minPlayers and now - oldestAt >= waitSeconds. Otherwise leave its parties in waiting, preserving join order.

Other ways pros do this

OptionWhat it isWhen pros pick itTrade-offs
Pure Luau planner + MemoryStoreQueue + MessagingServiceBeacon Yard's m11 architecture: pure matching logic in MatchRules wrapped by thin Roblox service calls in QueueService.Custom party rules, skill buckets, or deterministic unit tests in Jest-Lua.You manage queue expiry, hide windows and cross-server teleport coordination yourself.
Roblox MatchmakingServiceManaged engine matchmaking with configurable scoring attributes.Large games that want Roblox to group players into existing servers automatically without a custom lobby place.Less control over custom lobby scenes, party-split edge cases and reserved-server handoffs.
Choose one

Queue: Party A {1,2,3} at t=100, Party B {4,5} at t=101, Party C {6} at t=102. With now=105, maxSize=4, minPlayers=2, waitSeconds=20, what does planMatches return?

Queue: Party A {1,2,3} at t=100, Party B {4,5} at t=101, Party C {6} at t=102. With now=105, maxSize=4, minPlayers=2, waitSeconds=20, what does planMatches return?

Press 1 to 4 to choose, Enter to check.

Choose one

A 5-player party sits at the front of a queue whose maxSize is 4, followed by two 2-player parties. What should planMatches do?

A 5-player party sits at the front of a queue whose maxSize is 4, followed by two 2-player parties. What should planMatches do?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 4 of 5

Build and grade the atomic party matchmaker

Write planMatches in the browser cell below. To grade it in Studio, copy it into challenges/capstone_party_queue_work.luau, press Play and run game.ServerScriptService.RunGameTests:Invoke("capstone_party_queue", "capstone-party-queue") in the server command bar.

Luau
Ctrl Enter runs the cell. Checked by tests

Level 3, Professional

Challenge

Pass the browser cell above (and optionally challenges/capstone_party_queue.luau in Studio). Solution: challenges/solutions/capstone-systems.md.

Start from
The game at milestone m14 (download)
Check it here
Run the code cell; the challenge completes when it passes. No Studio needed.
Or grade it in Studio
In Studio, press Play, then run game.ServerScriptService.RunGameTests:Invoke("capstone_party_queue", "capstone-party-queue") in the server command bar. Copy the pass code from Output.

Done means

  • A 3-player party followed by a 2-player party and a solo produces match {1,2,3,6} and leaves {4,5} waiting intact
  • After waitSeconds, a partial group with at least minPlayers launches; a group below minPlayers stays waiting
  • An oversized party (> maxSize) stays in waiting and does not block smaller parties behind it
  • Duplicate userIds across or inside parties count once (first appearance wins), and caller input tables are never mutated
Have a Studio pass code?

The Course Runner prints this code when the test passes.

Choose one

Why do studios keep both the receipt decision logic and the party planner in pure Luau modules with zero Roblox service calls?

Why do studios keep both the receipt decision logic and the party planner in pure Luau modules with zero Roblox service calls?

Press 1 to 4 to choose, Enter to check.

Link to this step · This confused me

Step 5 of 5

Recap

Retiring the analogy

You no longer need “a courier who keeps coming back until you sign”. ProcessReceipt is one callback, set once from a server script (BindReceiptHandler is the newer route). Record the purchase id in the same save as the grant, then return PurchaseGranted. NotProcessedYet leaves the receipt unresolved: it returns on a rejoin or the next purchase, not on a timer.

Recap

  1. Project 5 builds a multi-product receipt router that rejects unknown or zero products, deduplicates analytics SKUs on replays, and rolls back cleanly on save or receipt-write failure.
  2. Project 6 builds an atomic party matchmaker from scratch that never splits friend groups, avoids head-of-line blocking on oversized parties, and leaves caller tables untouched.

ProcessReceipt is a courier who keeps coming back until you sign, until you decide what a duplicate looks like: without a stored purchase id, a redelivered receipt grants twice, and a record saved apart from the coins leaves a gap.

Next Capstone, part 4: ship it to staging (private publish, CI and deploy)

Link to this step · This confused me