How do I fix this code for developer product purchase?
I have tried making a Donation dev product. This is the code that I want to run when at least one of the products have been bought. Any solution? Thanks.
01 | local MarketplaceService = game:GetService( "MarketplaceService" ) |
02 | local DataStoreService = game:GetService( "DataStoreService" ) |
03 | local Players = game:GetService( "Players" ) |
05 | local purchaseHistoryStore = DataStoreService:GetDataStore( "PurchaseHistory" ) |
07 | local productFunctions = { } |
08 | productFunctions [ 1082181820 ] = function (receipt, player) |
09 | game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "10" ) |
12 | productFunctions [ 1082181849 ] = function (receipt, player) |
13 | game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "50" ) |
16 | productFunctions [ 1082181878 ] = function (receipt, player) |
17 | game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "100" ) |
20 | productFunctions [ 1082181916 ] = function (receipt, player) |
21 | game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "150" ) |
24 | productFunctions [ 1082181958 ] = function (receipt, player) |
25 | game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "200" ) |
28 | productFunctions [ 1082181998 ] = function (receipt, player) |
29 | game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "500" ) |
32 | productFunctions [ 1082182029 ] = function (receipt, player) |
33 | game.ReplicatedStorage.Rem_Events.DonationReceived:FireAllClients(player, "1000" ) |
36 | local function processReceipt(receiptInfo) |
38 | local playerProductKey = receiptInfo.PlayerId .. "_" .. receiptInfo.PurchaseId |
39 | local purchased = false |
40 | local success, errorMessage = pcall ( function () |
41 | purchased = purchaseHistoryStore:GetAsync(playerProductKey) |
43 | if success and purchased then |
44 | return Enum.ProductPurchaseDecision.PurchaseGranted |
45 | elseif not success then |
46 | error ( "Data store error:" .. errorMessage) |
48 | local player = Players:GetPlayerByUserId(receiptInfo.PlayerId) |
50 | return Enum.ProductPurchaseDecision.NotProcessedYet |
53 | local handler = productFunctions [ receiptInfo.ProductId ] |
55 | local success, result = pcall (handler, receiptInfo, player) |
56 | if not success or not result then |
57 | warn( "Error occurred while processing a product purchase" ) |
58 | return Enum.ProductPurchaseDecision.NotProcessedYet |
61 | local success, errorMessage = pcall ( function () |
62 | purchaseHistoryStore:SetAsync(playerProductKey, true ) |
65 | error ( "Cannot save purchase data: " .. errorMessage) |
68 | return Enum.ProductPurchaseDecision.PurchaseGranted |
71 | MarketplaceService.ProcessReceipt = processReceipt |