Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Table setup containing product IDs and functions for handling purchases
- local ProductFunctions = {}
- local Settings = {
- Duration = 10,
- GrowScale = 1,
- TweenDuration = .75,
- ShrinkScale = 0,
- NotProcessedYet = Enum.ProductPurchaseDecision.NotProcessedYet,
- PurchaseGranted = Enum.ProductPurchaseDecision.PurchaseGranted,
- PurchaseHistoryDataStoreKey = "PurchaseHistory", -- PurchaseHistoryDataStoreName
- }
- local MarketplaceService = game:GetService("MarketplaceService")
- local TweenService = game:GetService("TweenService")
- local DataStoreService = game:GetService("DataStoreService")
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Modules = ReplicatedStorage:WaitForChild("Modules")
- local ProductIDS = require(Modules:WaitForChild("ProductIDS"))
- -- MODULE SCRIPT CODE
- --[[
- local ProductIDS = {}
- ProductIDS.TestID = 1817343951
- return ProductIDS
- ]]
- -- Data store for tracking purchases that were successfully processed
- local PurchaseHistoryDataStore = DataStoreService:GetDataStore(Settings.PurchaseHistoryDataStoreKey)
- local TweenPlayerInfo = TweenInfo.new(Settings.TweenDuration)
- -- ProductId ProductIDS.TestID For a full Shrink Toggles
- ProductFunctions[ProductIDS.TestID] = function(Receipt, Player : (Player))
- -- Logic/code for player buying a full toggle shink/grow (may vary)
- local Character = Player.Character or Player.CharacterAdded:Wait()
- local Humanoid = Character:FindFirstChild("Humanoid")
- if Character and Humanoid and Humanoid ~= nil then
- -- Shrinks The Player to stick man
- TweenService:Create(Humanoid.BodyTypeScale, TweenPlayerInfo, {Value = Settings.ShrinkScale}):Play()
- TweenService:Create(Humanoid.BodyWidthScale, TweenPlayerInfo, {Value = Settings.ShrinkScale}):Play()
- TweenService:Create(Humanoid.HeadScale, TweenPlayerInfo, {Value = Settings.ShrinkScale}):Play()
- task.delay(Settings.Duration, function()
- TweenService:Create(Humanoid.BodyTypeScale, TweenPlayerInfo, {Value = Settings.GrowScale}):Play()
- TweenService:Create(Humanoid.BodyWidthScale, TweenPlayerInfo, {Value = Settings.GrowScale}):Play()
- TweenService:Create(Humanoid.HeadScale, TweenPlayerInfo, {Value = Settings.GrowScale}):Play()
- end)
- end
- -- Indicate a successful purchase
- return true
- end
- -- The core 'ProcessReceipt' callback function
- local ProcessReceipt = function(ReceiptInfo)
- -- Determine if the product was already granted by checking the data store
- local PlayerProductKey = ReceiptInfo.PlayerId .. "_" .. ReceiptInfo.PurchaseId
- local Success, Result, ErrorMessage, IsPurchaseRecorded
- local Purchased = false
- Success, ErrorMessage = pcall(function()
- Purchased = PurchaseHistoryDataStore:GetAsync(PlayerProductKey)
- end)
- -- If purchase was recorded, the product was already granted
- if Success and Purchased then
- return Settings.PurchaseGranted
- elseif not Success then
- error("Data store error:" .. ErrorMessage)
- end
- -- Determine if the product was already granted by checking the data store
- Success, IsPurchaseRecorded = pcall(function()
- return PurchaseHistoryDataStore:UpdateAsync(PlayerProductKey, function(AlreadyPurchased)
- if AlreadyPurchased then
- return true
- end
- -- Find the player who made the purchase in the server
- local Player = 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 nil
- end
- local Handler = ProductFunctions[ReceiptInfo.ProductId]
- local Success, Result = pcall(Handler, ReceiptInfo, Player)
- if not Success or not Result then
- -- If granting the product failed, do NOT record the purchase in datastores.
- error("Failed to process a product purchase for ProductId: " .. tostring(ReceiptInfo.ProductId) .. " Player: " .. tostring(Player) .. " Error: " .. tostring(Result))
- return nil
- end
- -- Record the transcation in purchaseHistoryStore.
- return true
- end)
- end)
- if not Success then
- error("Failed to process receipt due to data store error.")
- return Settings.NotProcessedYet
- elseif IsPurchaseRecorded == nil then
- -- Didn't update the value in data store.
- return Settings.NotProcessedYet
- else
- -- IMPORTANT: Tell Roblox that the game successfully handled the purchase
- return Settings.PurchaseGranted
- end
- end
- -- Set the callback; this can only be done once by one script on the server!
- MarketplaceService.ProcessReceipt = ProcessReceipt
Advertisement
Advertisement