Whenever somebody purchases something in our game half the time they don’t receive the yen. In studio this never happens, but in game it doesn’t. I’ve been trying to solve this problem for a while, but I’m having difficulty because I don’t see any problems in the code. Any suggestions on what may be going wrong?
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local purchaseHistoryStore = DataStoreService:GetDataStore("PurchaseHistory")
local productFunctions = {}
productFunctions[899176559] = function(receipt,player)
local Yen = player:WaitForChild("Data"):WaitForChild("Yen")
if Yen then
Yen.Value = Yen.Value + 1000
return true
end
end
productFunctions[899217110] = function(receipt,player)
local Yen = player:WaitForChild("Data"):WaitForChild("Yen")
if Yen then
Yen.Value = Yen.Value + 10000
return true
end
end
productFunctions[899218613] = function(receipt,player)
local Yen = player:WaitForChild("Data"):WaitForChild("Yen")
if Yen then
Yen.Value = Yen.Value + 15000
return true
end
end
productFunctions[899210277] = function(receipt,player)
local Yen = player:WaitForChild("Data"):WaitForChild("Yen")
if Yen then
Yen.Value = Yen.Value + 2000
return true
end
end
productFunctions[899212133] = function(receipt,player)
local Yen = player:WaitForChild("Data"):WaitForChild("Yen")
if Yen then
Yen.Value = Yen.Value + 4000
return true
end
end
productFunctions[899213955] = function(receipt,player)
local Yen = player:WaitForChild("Data"):WaitForChild("Yen")
if Yen then
Yen.Value = Yen.Value + 6000
return true
end
end
local function processReceipt(receiptInfo)
-- Determine if the product was already granted by checking the data store
print("[Processing Purchase]")
local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId
local purchased = false
local success, errorMessage = pcall(function()
purchased = purchaseHistoryStore:GetAsync(playerProductKey)
end)
-- If purchase was recorded, the product was already granted
if success and purchased then
print("[Purcahse Was Successfull]")
return Enum.ProductPurchaseDecision.PurchaseGranted
elseif not success then
error("Data store error:" .. errorMessage)
end
-- Find the player who made the purchase in the server
local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
if not player then
-- The player probably left the game
-- If they come back, the callback will be called again
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- Look up handler function from 'productFunctions' table above
print("[Giving Yen]")
local handler = productFunctions[receiptInfo.ProductId]
-- Call the handler function and catch any errors
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
-- Record transaction in data store so it isn't granted again
local success, errorMessage = pcall(function()
purchaseHistoryStore:SetAsync(playerProductKey, true)
end)
if not success then
error("Cannot save purchase data: " .. errorMessage)
end
-- IMPORTANT: Tell Roblox that the game successfully handled the purchase
return Enum.ProductPurchaseDecision.PurchaseGranted
end
-- Set the callback; this can only be done once by one script on the server!
MarketplaceService.ProcessReceipt = processReceipt