I have this code that handles dev product purchases, i’ve spent hours on it and I can’t fix it, sometimes it works, sometimes it doesn’t. I have no clue why
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game.Players
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayersPurchased = {}
productFunctions = {}
task.wait(2)
productFunctions[1845222983] = function(receipt, player)
ReplicatedStorage.CrateEvents.Crate1Purchase:FireClient(player)
PlayersPurchased[player.Name] = true
end
task.wait(2)
productFunctions[1845223224] = function(receipt, player)
ReplicatedStorage.CrateEvents.Crate2Purchase:FireClient(player)
PlayersPurchased[player.Name] = true
end
task.wait(2)
productFunctions[1845223470] = function(receipt, player)
ReplicatedStorage.CrateEvents.Crate3Purchase:FireClient(player)
PlayersPurchased[player.Name] = true
end
ReplicatedStorage.GrantMoney.OnServerEvent:Connect(function(player, award)
if PlayersPurchased[player.Name] then
PlayersPurchased[player.Name] = nil
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + award
else
player:Kick("Exploiting")
end
end)
print("ready")
local function processReceipt(receiptInfo)
print("ran")
local userId = receiptInfo.PlayerId
local productId = receiptInfo.ProductId
local player = Players:GetPlayerByUserId(userId)
if player then
-- Get the handler function associated with the developer product ID and attempt to run it
local handler = productFunctions[productId]
local success, result = pcall(handler, receiptInfo, player)
if success then
-- The user has received their benefits
-- Return "PurchaseGranted" to confirm the transaction
return Enum.ProductPurchaseDecision.PurchaseGranted
else
warn("Failed to process receipt:", receiptInfo, result)
end
end
-- The user's benefits couldn't be awarded
-- Return "NotProcessedYet" to try again next time the user joins
return Enum.ProductPurchaseDecision.NotProcessedYet
end
-- Set the callback; this can only be done once by one server-side script
MarketplaceService.ProcessReceipt = processReceipt
any help is appreciated