Is there a way to improve this developer product handler?

So I was having trouble with developer products because I was using more than 1 receiptInfo, so I combined both my donation board script and my obby skip stage developer product into one script.

I tested the skip stage dev product and it works as intended.

My questions are the following: Is this method going to ensure that the skip stage dev product will always work? Will it break if too many people buy it in a small amount of time? Is there any way to improve it?

Here is the receiptInfo part of the code (I took off the other part because it was the donation board):

function ReceiptHandler()
	warn("Donation Board: ProcessReceipt Activated")
	game:GetService("MarketplaceService").ProcessReceipt = function(receiptInfo)
		local plrPurchased = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
		local devProductID = 1190837096

		if not plrPurchased then 
			return Enum.ProductPurchaseDecision.NotProcessedYet 
		end


		if receiptInfo.ProductId == devProductID then


			if tonumber(plrPurchased.leaderstats.Stage.Value) < #workspace.Stages:GetChildren() - 1 then

				plrPurchased.leaderstats.Stage.Value = plrPurchased.leaderstats.Stage.Value + 1


			else

				--plrPurchased.leaderstats.Stage.Value = "End"
				game.StarterGui.SkipStage.TextButton.Visible = false
				plrPurchased.leaderstats.Stage.Value = plrPurchased.leaderstats.Stage.Value + 1
			end


			plrPurchased:LoadCharacter()


			return Enum.ProductPurchaseDecision.PurchaseGranted
		end
		local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_product_" .. receiptInfo.ProductId
		local numberBought = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory"):IncrementAsync(playerProductKey, 1)
		local ProductBought = game:GetService("DataStoreService"):GetDataStore("PurchaseHistoryCount"):IncrementAsync(receiptInfo.ProductId, 1)
		local PlayerFound = false
		for i, v in pairs (game.Players:GetChildren()) do
			if v:IsA('Player') then
				if v.userId == receiptInfo.PlayerId then
					for _, p in pairs (Products.Products) do
						if p.ProductId == receiptInfo.ProductId then
							if v ~= nil then
								PlayerFound = true
								game:GetService("DataStoreService"):GetOrderedDataStore(GetData()):IncrementAsync(receiptInfo.PlayerId, p.ProductPrice)
							end
						end
					end
				end
			end
		end
		if PlayerFound ~= true then
			return Enum.ProductPurchaseDecision.NotProcessedYet 
		else
			return Enum.ProductPurchaseDecision.PurchaseGranted		
		end
	end	
end
1 Like

I would change this to plrPurchased.leaderstats.Stage.Value += 1. It’s luau syntax. Also, not sure if this would make it more efficient or not but I’d do an if statement inside of if tonumber(plrPurchased.leaderstats.Stage.Value) < #workspace.Stages:GetChildren() - 1 then and remove the -1 so you don’t have to repeat plrPurchased.leaderstats.Stage.Value += 1 but that’s just personal preference I guess.

I don’t see anything wrong with the rest of the code, but I’m not really an expert at scripting so maybe I’m missing something. I hope this helps!

2 Likes

make this a local variable, it is used multiple times

make the first one

if v then

and the second one

if not PlayerFound then

plrPurchased.leaderstats.Stage.Value += 1 is shorter
also define Stage as a local variable, you use it multiple times

hope this helps

1 Like