Register Login

Example game

A tiny experience that sells one developer product, built on the SDK.

The full example game

A tiny experience that sells one developer product ("100 coins"), built on the SDK. Its receipt handler shows the real form: the coins come from the game's own ExampleGrants module, never from R3FTO.

Its source is in the SDK repository, in examples/minimal-developer-product. Open it with Rojo (rojo serve or rojo build), or add each script below in the place its label names, next to R3FTO and R3FTOStart. Set your developer product ID in BuyButton. Everything else runs as is.

ServerScriptService/ReceiptHandler · Script
-- Script · ServerScriptService/ReceiptHandler
-- The example game's ONE receipt handler. R3FTO adds a single line to it and changes nothing else.

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")

local R3FTO = require(ServerScriptService:WaitForChild("R3FTO"))
local ExampleGrants = require(ServerScriptService:WaitForChild("ExampleGrants"))

Players.PlayerAdded:Connect(ExampleGrants.setUpPlayer)
for _, player in Players:GetPlayers() do
	ExampleGrants.setUpPlayer(player)
end

MarketplaceService.ProcessReceipt = function(receiptInfo)
	-- R3FTO: report the receipt. Returns at once, never yields and never changes your decision below.
	pcall(R3FTO.observeReceipt, receiptInfo)

	-- Your own product delivery and your own decision, exactly as before.
	return ExampleGrants.grant(receiptInfo)
end
ServerScriptService/ExampleGrants · ModuleScript
-- ModuleScript · ServerScriptService/ExampleGrants
-- THE EXAMPLE GAME'S OWN product delivery. This is not R3FTO code and R3FTO never calls it.
--
-- It follows Roblox's recommended pattern: grant the product and remember the PurchaseId in the same durable write,
-- return PurchaseGranted only after that write succeeded, and NotProcessedYet otherwise (Roblox asks again later).
-- Your game already has its own version of this. Keep yours.

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local ExampleGrants = {}

ExampleGrants.COINS_PER_PURCHASE = 100

local purchases = DataStoreService:GetDataStore("ExampleGame_PlayerCoins")

local function coinsValue(player: Player): IntValue?
	local stats = player:FindFirstChild("leaderstats")
	return if stats then stats:FindFirstChild("Coins") :: IntValue? else nil
end

function ExampleGrants.setUpPlayer(player: Player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = stats
	stats.Parent = player

	local ok, saved = pcall(function()
		return purchases:GetAsync(tostring(player.UserId))
	end)
	if ok and type(saved) == "table" and type(saved.coins) == "number" then
		coins.Value = saved.coins
	end
end

-- Grants the coins once per PurchaseId. Returns the Roblox decision.
function ExampleGrants.grant(receiptInfo): Enum.ProductPurchaseDecision
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if player == nil then
		return Enum.ProductPurchaseDecision.NotProcessedYet -- Roblox asks again when the player is back
	end

	local newTotal = nil
	local ok = pcall(function()
		purchases:UpdateAsync(tostring(receiptInfo.PlayerId), function(saved)
			saved = if type(saved) == "table" then saved else { coins = 0, processed = {} }
			saved.processed = saved.processed or {}
			if saved.processed[receiptInfo.PurchaseId] then
				newTotal = saved.coins
				return nil -- already granted: keep the data as it is
			end
			saved.processed[receiptInfo.PurchaseId] = true
			saved.coins += ExampleGrants.COINS_PER_PURCHASE
			newTotal = saved.coins
			return saved
		end)
	end)
	if not ok then
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	local coins = coinsValue(player)
	if coins and newTotal then
		coins.Value = newTotal
	end
	return Enum.ProductPurchaseDecision.PurchaseGranted
end

return ExampleGrants
StarterPlayer/StarterPlayerScripts/BuyButton · LocalScript
-- LocalScript · StarterPlayer/StarterPlayerScripts/BuyButton
-- A minimal purchase button. It holds no secret and no R3FTO code: purchases are reported only on the server.

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

local DEVELOPER_PRODUCT_ID = 0 -- replace with your developer product id (Creator Hub > Monetization > Developer Products)

local player = Players.LocalPlayer
local gui = Instance.new("ScreenGui")
gui.Name = "ExampleShop"
gui.ResetOnSpawn = false

local button = Instance.new("TextButton")
button.Size = UDim2.fromOffset(220, 48)
button.Position = UDim2.new(0.5, -110, 1, -80)
button.Text = "Buy 100 coins"
button.Parent = gui

button.Activated:Connect(function()
	if DEVELOPER_PRODUCT_ID == 0 then
		warn("[Example] Set DEVELOPER_PRODUCT_ID in BuyButton first.")
		return
	end
	MarketplaceService:PromptProductPurchase(player, DEVELOPER_PRODUCT_ID)
end)

gui.Parent = player:WaitForChild("PlayerGui")
ServerScriptService/R3FTOTestDemos · Script
-- Script · ServerScriptService/R3FTOTestDemos (optional; delete it before you publish to Production)
-- Runs SIMULATED receipts against the R3FTO Test environment so you can see the SDK work without a real purchase.
-- Simulated receipts are marked simulated end to end; R3FTO refuses them in Production. They prove nothing about real
-- Roblox purchases.

local ServerScriptService = game:GetService("ServerScriptService")

local R3FTO = require(ServerScriptService:WaitForChild("R3FTO"))

local RUN_DEMOS = true
local DEMO_PRODUCT_ID = 1 -- any whole number for a simulation
local DEMO_USER_ID = 1 -- any whole number for a simulation

if not RUN_DEMOS then
	return
end

task.wait(2) -- let R3FTOStart run first
local diagnostics = R3FTO.getDiagnostics()
if diagnostics.environment ~= "test" then
	print("[Example] Test demos run only when R3FTOStart uses the test environment.")
	return
end

-- 1. One simulated receipt, observed twice: the second call joins the first one's work (a duplicate callback).
local first = R3FTO.simulateTestReceipt({
	userId = DEMO_USER_ID,
	productId = DEMO_PRODUCT_ID,
	currencySpent = 25,
	simulationId = "example-" .. string.sub(game.JobId, 1, 8),
})
local again = R3FTO.simulateTestReceipt({
	userId = DEMO_USER_ID,
	productId = DEMO_PRODUCT_ID,
	currencySpent = 25,
	simulationId = "example-" .. string.sub(game.JobId, 1, 8),
})
print("[Example] simulated receipt", first.purchaseId, "accepted:", first.accepted, "duplicate on second call:", again.duplicate)

-- 2. A few seconds later, show what happened to it.
task.wait(5)
local status = R3FTO.getReceiptStatus(first.purchaseId)
if status then
	print("[Example] durability:", status.durability)
	print("[Example] DataStore copy:", status.channels.storage.state, status.channels.storage.lastError or "")
	print("[Example] R3FTO delivery:", status.channels.delivery.state, status.channels.delivery.lastError or "")
end
print("[Example] diagnostics:", game:GetService("HttpService"):JSONEncode(R3FTO.getDiagnostics()))

R3FTOTestDemos is optional and runs only in Test. It sends one simulated receipt twice. Expected in the Output: duplicate on second call: true and durability: delivered. Delete it before you go live.

To see failures safely in Test: add this line to the start settings (delivery fails, the DataStore copy still works), or turn off Enable Studio Access to API Services (the DataStore fails, delivery still works). Change them back afterwards.

In the start settings, Test only
apiBaseUrl = "https://invalid.example"

Documentation

Back to site