Thoughts on product info script

This is my first time doing a product info script and I was just wondering if there is anything I can touch up!

P.S - I dont store playerdata inside of Players but instead in a folder in ReplicatedStorage just in case if anyone was wondering

-- services

local MarketplaceService = game:GetService("MarketplaceService")
local Players  			 = game:GetService("Players")
local ReplicatedStorage  = game:GetService("ReplicatedStorage")

-- constants

local PLAYER_DATA  = ReplicatedStorage.PlayerData

local PRODUCTS_IDS = {
	[1187687288]   = {
			Amount = 1000,
			Repeat = true,
			Class  = "Cash",
	},
	
}

local PASS_EVENTS  = {
	[188768734288] = function(player: Player)
						warn("this is a test!")
					 end,
}

-- functions

function product_Receipt(receiptInfo)

	local purchased  = false
	
	local product_Class  = PRODUCTS_IDS[receiptInfo.ProductId].Class
	local product_Repeat = PRODUCTS_IDS[receiptInfo.ProductId].Repeat
	local product_Amount = PRODUCTS_IDS[receiptInfo.ProductId].Amount
	
	local success,error = pcall(function()
		purchased       = not MarketplaceService:UserOwnsGamePassAsync(receiptInfo.PlayerId,receiptInfo.ProductId) and true or product_Repeat and true
	end)
	
	
	local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if not player then -- player left the game
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	if success and purchased then
		if product_Repeat then
			if PLAYER_DATA:FindFirstChild(player.Name) then
				local PlayerData = PLAYER_DATA[player.Name]
					PlayerData.Currency[product_Class].Value += product_Amount
				return Enum.ProductPurchaseDecision.PurchaseGranted
			end 
		else
			local success,error = pcall(function()
				PASS_EVENTS[receiptInfo.ProductId]()
			end)
			
			if success then
				return Enum.ProductPurchaseDecision.PurchaseGranted
			end
		end
	end
	
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

-- initalize

MarketplaceService.ProcessReceipt = product_Receipt
2 Likes

The only thing that I see is the unused ‘error’ variables, which could just not be specified (local success,_ = pcall(function())

Thank you, didn’t even think about that :smiley: :+1:

1 Like