Hey! I’m trying to make it so whenever you buy a developer product your “Spent” Leaderstat increases by the price of the product.
The issue is I’m keep getting an error when I purchase the product.
I looked for solutions but I don’t really know what to search up and I don’t know what the problem is.
Here’s the code, I got the ProcessReceipt function from the MarketplaceService.ProcessReceipt article
(I’m extremely new to Datastores and MarketplaceService so this might be simple for you)
----------
local ID = 1236498109 -- // Developer product ID
----------
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Name = "leaderstats"
Leaderstats.Parent = Player
local Spent = Instance.new("IntValue")
Spent.Name = "Spent"
Spent.Value = 0
Spent.Parent = Leaderstats
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Spent.Value = Data
end
end)
local Asset = game:GetService("MarketplaceService"):GetProductInfo(ID)
local Price = Asset.PriceInRobux
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
local productFunctions = {}
productFunctions[1236498109] = function(receipt, player)
local stats = player:FindFirstChild("leaderstats")
local spent = stats and stats:FindFirstChild("Spent")
if spent and Price then
spent.Value += Price
print(receipt)
return true
end
end
local function processReceipt(receiptInfo)
local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
local purchased = false
local success, errorMessage = pcall(function()
purchased = purchaseHistoryStore:GetAsync(playerProductKey)
end)
if success and purchased then
return Enum.ProductPurchaseDecision.PurchaseGranted
elseif not success then
error("Data store error:" .. errorMessage)
end
local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local handler = productFunctions[receiptInfo.ProductId]
local success, result = pcall(handler, receiptInfo, player)
if not success or not result then
warn("Error occurred while processing a product purchase")
print("\nProductId:", receiptInfo.ProductId)
print("\nPlayer:", player)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
local success, errorMessage = pcall(function()
purchaseHistoryStore:SetAsync(playerProductKey, true)
end)
if not success then
error("Cannot save purchase data: " .. errorMessage)
end
return Enum.ProductPurchaseDecision.PurchaseGranted
end
MarketplaceService.ProcessReceipt = processReceipt
That’s the code. It errors on:
local success, result = pcall(handler, receiptInfo, player)
if not success or not result then
warn("Error occurred while processing a product purchase")
print("\nProductId:", receiptInfo.ProductId)
print("\nPlayer:", player)
return Enum.ProductPurchaseDecision.NotProcessedYet
end
Any help is appreciated! Thank you so much!