Product purchase error

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!

Just wanted to say, I get the same error when and when I’m not in studio

Does anyone know how to do this I just used the code from MarketplaceService.ProcessReceipt

it has to be

Players.PlayerAdded:Connect(function(player)

Line 2 can be

Players.PlayerAdded:Connect(function(player)
    local Leaderstats = Instance.new("Folder", player)

you have no variable for “Player”

Thanks for the response but this still didn’t solve my problem. I still really need to fix this but I can’t find any solutions, if anyone knows how to do this please tell me.

I’m no scripter, but I think you could fix it by just not adding that script.

I entirely copied the script that Roblox made and added leaderstats to it and made my own function which doesn’t really have to do anything with the processReceipt function erroring. And without that script it doesn’t do anything.

See but without the script there isn’t a problem.

What error are you receiving? Screenshot would be fine.

Without the script nothing happens though and the productFunctions function is useless, as the processReceipt one calls it whenever a purchase is made

Yes, he is messing around with you.

1 Like