Purchase Processing Script not working

I made a script to add Cash to a players account when the buy a DevProduct, but sometimes it adds double the amount of cash that It should give.
My Script:

-- setup local variables
local MarketplaceService = game:GetService("MarketplaceService")

local gem1 = 1261236496
local gem2 = 1261237370
local gem3 = 1261237496
local gem4 = 1261237607
local gem5 = 1261237740
local gem6 = 1261237862
local gem7 = 1261237980
MarketplaceService.ProcessReceipt = function(receiptInfo) 

	for i, player in ipairs(game.Players:GetChildren()) do
		if player.userId == receiptInfo.PlayerId then

			if receiptInfo.ProductId == gem1 then
				player.Cash.Value = player.Cash.Value + 2000
			end
			if receiptInfo.ProductId == gem2 then
				player.Cash.Value = player.Cash.Value + 5000
			end
			if receiptInfo.ProductId == gem3 then
				player.Cash.Value = player.Cash.Value + 10000
			end
			if receiptInfo.ProductId == gem4 then
				player.Cash.Value = player.Cash.Value + 50000
			end
			if receiptInfo.ProductId == gem5 then
				player.Cash.Value = player.Cash.Value + 100000
			end
			if receiptInfo.ProductId == gem6 then
				player.Cash.Value = player.Cash.Value + 500000
			end
			if receiptInfo.ProductId == gem7 then
				player.Cash.Value = player.Cash.Value + 1000000
			end					
		end
	end
end


return Enum.ProductPurchaseDecision.PurchaseGranted		


What’s Wrong?

Are there any errors in the output window? I’m not sure how to help you on this. You could try replacing the
end
if then
with elseif. Even if that doesn’t work, it would still clean up your script a little bit.

ProcessReceipt gets fired when a DevProduct is bought, you are connecting it every time a Player joins and don’t disconnect it when they leave, not to mention this is very bad practice.

1 Like

You can just get the player by userId.
Players:GetPlayerByUserId()

local MarketplaceService = game:GetService("MarketplaceService")

local gem1 = 1261236496
local gem2 = 1261237370
local gem3 = 1261237496
local gem4 = 1261237607
local gem5 = 1261237740
local gem6 = 1261237862
local gem7 = 1261237980

MarketplaceService.ProcessReceipt = function(receiptInfo) 
	local player = game.Players:GetPlayerByUserId(receiptInfo.PlayerId)
	if player then
		if receiptInfo.ProductId == gem1 then
			player.Cash.Value = player.Cash.Value + 2000
		end
		if receiptInfo.ProductId == gem2 then
			player.Cash.Value = player.Cash.Value + 5000
		end
		if receiptInfo.ProductId == gem3 then
			player.Cash.Value = player.Cash.Value + 10000
		end
		if receiptInfo.ProductId == gem4 then
			player.Cash.Value = player.Cash.Value + 50000
		end
		if receiptInfo.ProductId == gem5 then
			player.Cash.Value = player.Cash.Value + 100000
		end
		if receiptInfo.ProductId == gem6 then
			player.Cash.Value = player.Cash.Value + 500000
		end
		if receiptInfo.ProductId == gem7 then
			player.Cash.Value = player.Cash.Value + 1000000
		end					
	end
end
return Enum.ProductPurchaseDecision.PurchaseGranted	
2 Likes