I modified the process receipt callback function from Roblox’ documentation and am wondering if it can be improved at all. I can see things getting a bit messy down the line with this when more things get added.
local Players = game:GetService("Players")
local MarketplaceService = game:GetService("MarketplaceService")
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Manager = require(ServerScriptService.PlayerData.Manager) --data saving using ProfileService
local TemporaryBoosts = require(ReplicatedStorage.Databases.TemporaryBoosts) --dictionary
local PermanentBoosts = require(ReplicatedStorage.Databases.PermanentBoosts) --dictionary
local remotes = ReplicatedStorage.Remotes
local permanentBoostIDs = {}
local temporaryBoostIDs = {}
for _, permanentBoost in PermanentBoosts do
table.insert(permanentBoostIDs, permanentBoost.ID)
end
for _, temporaryBoost in TemporaryBoosts do
table.insert(temporaryBoostIDs, temporaryBoost.ID)
end
local productFunctions = {}
productFunctions["ID"] = function(receipt, player) --left this here incase something comes up that needs it later in development
local profile = Manager.Profiles[player]
if not profile then return end
end
local function processReceipt(receiptInfo)
local userId = receiptInfo.PlayerId
local productId = receiptInfo.ProductId
local info = MarketplaceService:GetProductInfo(productId, Enum.InfoType.Product)
local player = Players:GetPlayerByUserId(userId)
if player then
if table.find(permanentBoostIDs, productId) then
local profile = Manager.Profiles[player]
if not profile then return end
local success, result = pcall(function()
for _, permanentBoost in PermanentBoosts do
if permanentBoost.ID == productId then
Manager.SetPermanentBoost(player, permanentBoost.boostAmount)
remotes.permanentBoostEvent:Fire(player)
end
end
end)
if success then
print("[".. player.Name.. "] has bought [".. info.Name.. "]")
return Enum.ProductPurchaseDecision.PurchaseGranted
else
warn("Failed to process receipt: ", receiptInfo, result)
end
elseif table.find(temporaryBoostIDs, productId) then
local profile = Manager.Profiles[player]
if not profile then return end
local success, result = pcall(function()
for _, temporaryBoost in TemporaryBoosts do
if temporaryBoost.ID == productId then
Manager.GiveTemporaryBoost(player, temporaryBoost)
end
end
end)
if success then
print("[".. player.Name.. "] has bought [".. info.Name.. "]")
return Enum.ProductPurchaseDecision.PurchaseGranted
else
warn("Failed to process receipt: ", receiptInfo, result)
end
else
local handler = productFunctions[productId]
local success, result = pcall(handler, receiptInfo, player)
if success then
print(player.Name.. " has bought ".. info.Name)
return Enum.ProductPurchaseDecision.PurchaseGranted
else
warn("Failed to process receipt: ", receiptInfo, result)
end
end
return Enum.ProductPurchaseDecision.NotProcessedYet
end
end
MarketplaceService.ProcessReceipt = processReceipt