Advertisement
AndiGodGamersDev

Modified Roblox's Default ProcessReceipt Callback Code

Apr 28th, 2024 (edited)
144
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.50 KB | Source Code | 0 0
  1. -- Table setup containing product IDs and functions for handling purchases
  2. local ProductFunctions = {}
  3.  
  4. local Settings = {
  5.     Duration = 10,
  6.     GrowScale = 1,
  7.     TweenDuration = .75,
  8.     ShrinkScale = 0,
  9.    
  10.     NotProcessedYet = Enum.ProductPurchaseDecision.NotProcessedYet,
  11.     PurchaseGranted = Enum.ProductPurchaseDecision.PurchaseGranted,
  12.    
  13.     PurchaseHistoryDataStoreKey = "PurchaseHistory", -- PurchaseHistoryDataStoreName
  14. }
  15.  
  16. local MarketplaceService = game:GetService("MarketplaceService")
  17. local TweenService = game:GetService("TweenService")
  18. local DataStoreService = game:GetService("DataStoreService")
  19. local Players = game:GetService("Players")
  20. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  21. local Modules = ReplicatedStorage:WaitForChild("Modules")
  22. local ProductIDS = require(Modules:WaitForChild("ProductIDS"))
  23.  
  24. -- MODULE SCRIPT CODE
  25. --[[
  26.     local ProductIDS = {}
  27.  
  28.     ProductIDS.TestID = 1817343951
  29.  
  30.     return ProductIDS
  31. ]]
  32.  
  33. -- Data store for tracking purchases that were successfully processed
  34. local PurchaseHistoryDataStore = DataStoreService:GetDataStore(Settings.PurchaseHistoryDataStoreKey)
  35. local TweenPlayerInfo = TweenInfo.new(Settings.TweenDuration)
  36.  
  37. -- ProductId ProductIDS.TestID For a full Shrink Toggles
  38. ProductFunctions[ProductIDS.TestID] = function(Receipt, Player : (Player))
  39.     -- Logic/code for player buying a full toggle shink/grow (may vary)
  40.     local Character = Player.Character or Player.CharacterAdded:Wait()
  41.     local Humanoid = Character:FindFirstChild("Humanoid")
  42.     if Character and Humanoid and Humanoid ~= nil then
  43.         -- Shrinks The Player to stick man
  44.         TweenService:Create(Humanoid.BodyTypeScale, TweenPlayerInfo, {Value = Settings.ShrinkScale}):Play()
  45.         TweenService:Create(Humanoid.BodyWidthScale, TweenPlayerInfo, {Value = Settings.ShrinkScale}):Play()
  46.         TweenService:Create(Humanoid.HeadScale, TweenPlayerInfo, {Value = Settings.ShrinkScale}):Play()
  47.         task.delay(Settings.Duration, function()
  48.             TweenService:Create(Humanoid.BodyTypeScale, TweenPlayerInfo, {Value = Settings.GrowScale}):Play()
  49.             TweenService:Create(Humanoid.BodyWidthScale, TweenPlayerInfo, {Value = Settings.GrowScale}):Play()
  50.             TweenService:Create(Humanoid.HeadScale, TweenPlayerInfo, {Value = Settings.GrowScale}):Play()
  51.         end)
  52.     end
  53.     -- Indicate a successful purchase
  54.     return true
  55. end
  56.  
  57. -- The core 'ProcessReceipt' callback function
  58. local ProcessReceipt = function(ReceiptInfo)
  59.     -- Determine if the product was already granted by checking the data store
  60.     local PlayerProductKey = ReceiptInfo.PlayerId .. "_" .. ReceiptInfo.PurchaseId
  61.     local Success, Result, ErrorMessage, IsPurchaseRecorded
  62.  
  63.     local Purchased = false
  64.    
  65.     Success, ErrorMessage = pcall(function()
  66.         Purchased = PurchaseHistoryDataStore:GetAsync(PlayerProductKey)
  67.     end)
  68.    
  69.     -- If purchase was recorded, the product was already granted
  70.     if Success and Purchased then
  71.         return Settings.PurchaseGranted
  72.     elseif not Success then
  73.         error("Data store error:" .. ErrorMessage)
  74.     end
  75.    
  76.     -- Determine if the product was already granted by checking the data store  
  77.     Success, IsPurchaseRecorded = pcall(function()
  78.         return PurchaseHistoryDataStore:UpdateAsync(PlayerProductKey, function(AlreadyPurchased)
  79.             if AlreadyPurchased then
  80.                 return true
  81.             end
  82.            
  83.             -- Find the player who made the purchase in the server
  84.             local Player = Players:GetPlayerByUserId(ReceiptInfo.PlayerId)
  85.             if not Player then
  86.                 -- The player probably left the game
  87.                 -- If they come back, the callback will be called again
  88.                 return nil
  89.             end
  90.  
  91.             local Handler = ProductFunctions[ReceiptInfo.ProductId]
  92.            
  93.             local Success, Result = pcall(Handler, ReceiptInfo, Player)
  94.  
  95.             if not Success or not Result then
  96.                 -- If granting the product failed, do NOT record the purchase in datastores.
  97.                 error("Failed to process a product purchase for ProductId: " .. tostring(ReceiptInfo.ProductId) .. " Player: " .. tostring(Player) .. " Error: " .. tostring(Result))
  98.                 return nil
  99.             end
  100.            
  101.             -- Record the transcation in purchaseHistoryStore.
  102.             return true
  103.         end)
  104.     end)
  105.  
  106.     if not Success then
  107.         error("Failed to process receipt due to data store error.")
  108.         return Settings.NotProcessedYet
  109.     elseif IsPurchaseRecorded == nil then
  110.         -- Didn't update the value in data store.
  111.         return Settings.NotProcessedYet
  112.     else   
  113.         -- IMPORTANT: Tell Roblox that the game successfully handled the purchase
  114.         return Settings.PurchaseGranted
  115.     end
  116. end
  117.  
  118. -- Set the callback; this can only be done once by one script on the server!
  119. MarketplaceService.ProcessReceipt = ProcessReceipt
Tags: luau
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement