Developer Product Purchases - My mistake or not?

Hello my dev community,

recently I’m experiencing issues with my code:

local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")


local PreviousPurchases = DataStoreService:GetDataStore("PreviousPurchases")

local DevProduct = 1802119529

MarketplaceService.ProcessReceipt = function(receipt)

	-- Receipt has PurchaseId, PlayerId, ProductId, CurrencySpentValue, CurrencyType, PlaceIdWherePurchased

	local ID = receipt.PlayerId.."-"..receipt.PurchaseId

	local success = nil	

	pcall(function()
		success = PreviousPurchases:GetAsync(ID)
	end)        					

	if success then -- Has it already been bought ?
		-- Purchase has already been done
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	local player = game.Players:GetPlayerByUserId(receipt.PlayerId)

	if not player then
		-- Left, disconnected
		return Enum.ProductPurchaseDecision.NotProcessedYet -- We're going to give their rewards next time they join/ Next time fired
	else

		if receipt.ProductId == DevProduct then
			
			player.HeroFolder.H7O.Value = 2
			player.HeroFolder.HeroChosen.Value = 7.2

		end

		pcall(function()
			PreviousPurchases:SetAsync(ID, true)
		end)
		return Enum.ProductPurchaseDecision.PurchaseGranted

	end
end

the problem is that this code sometimes works, sometimes does not, it works again, then it does not again! so i need to know, is my code not efficient enough, or MarketplaceService has some issues,

The code suppose to check, when the devproduct is bought, it will set those stats respectively

any help will be really appreciated! Thank you!

You are using success instead of result in the GetAsync method. Success will inly determine if the API call has failed or not, you should instead check for both success and result:

local succ, result = pcall(function()
    return Datastore:GetAsync(keyname)
end)

if succ and result then -- indicates that they bought it

Oh I see it, thanks alot

I see, that the way I’m doing it would not always work!