[HELP]Product Purchase Giving Product twice

Hello there I have recently came across a bug in my script where when someone purchases the product they will receive it but when they but it another time they will get the same product that they bought previously including the current product that they are buying. Video is provided down in the post including the outputs.Any help appreciated thx


local script:

local button = script.Parent
local Id = button.Parent.ProductId.Value
local plyr = game.Players.LocalPlayer
local MarketPlaceService = game:GetService("MarketplaceService")
button.MouseButton1Click:Connect(function()
	MarketPlaceService:PromptProductPurchase(plyr, Id)
end)

script:

local plyrs = game:GetService("Players")
local MarketPlaceService = game:GetService("MarketplaceService")

local function processReceipt(receiptInfo)
	local plyrs = game:GetService("Players")
	local MarketPlaceService = game:GetService("MarketplaceService")
		local plr = plyrs:GetPlayerByUserId(receiptInfo.PlayerId)
		local productId = receiptInfo.ProductId
		local MembershipStatus = plr.Membershipstatus.Value
		if not plr then
			print("not going through")
             return Enum.ProductPurchaseDecision.NotProcessedYet
		end
		local ids = {[1232493039] = 500, [1232493120] = 1000, [1232493242] = 1500, [1232493507] = 4100, [1232493505] = 11000}
		local tokens = plr.PlayerValues.Tokens
		if MembershipStatus == false then
		tokens.Value = tokens.Value +ids[productId]
			print(ids[productId])
		elseif MembershipStatus == true then
			local add = ids[productId]/100
			tokens.Value = tokens.Value +(add*115) 
			print(add*115) 
			return Enum.ProductPurchaseDecision.PurchaseGranted
			end
	end
	
MarketPlaceService.ProcessReceipt = processReceipt
local plyrs = game:GetService("Players")
local MarketPlaceService = game:GetService("MarketplaceService")
local purchases = {}

local function processReceipt(receiptInfo)
	local plyrs = game:GetService("Players")
	local MarketPlaceService = game:GetService("MarketplaceService")
		local plr = plyrs:GetPlayerByUserId(receiptInfo.PlayerId)
		local productId = receiptInfo.ProductId
		local MembershipStatus = plr.Membershipstatus.Value
		if not plr then
			print("not going through")
             return Enum.ProductPurchaseDecision.NotProcessedYet
		end

		if purchases[plr.UserId] and purchases[plr.UserId][productId] then
			print("Product already purchased")
			return Enum.ProductPurchaseDecision.NotProcessedYet
		end

		if not purchases[plr.UserId] then
			purchases[plr.UserId] = {}
		end

		purchases[plr.UserId][productId] = true

		local ids = {[1232493039] = 500, [1232493120] = 1000, [1232493242] = 1500, [1232493507] = 4100, [1232493505] = 11000}
		local tokens = plr.PlayerValues.Tokens
		if MembershipStatus == false then
		tokens.Value = tokens.Value +ids[productId]
			print(ids[productId])
		elseif MembershipStatus == true then
			local add = ids[productId]/100
			tokens.Value = tokens.Value +(add*115) 
			print(add*115) 
			return Enum.ProductPurchaseDecision.PurchaseGranted
			end
	end
	
MarketPlaceService.ProcessReceipt = processReceipt
1 Like

I have tried your solution,However if somebody tries to rebuy the same product it would not work thus I think I will try other ways.Thanks for your help though.

Improving on @StraightScared’s code (you had the right idea) but the root cause of this was, when a membership status is false, you didn’t return a purchase granted, only if the membership status was true.

I’ve commented the code, pointing out issues and tips for future development, and the reasons why I’ve changed the code how I have, so please take the time to read the comments. :slight_smile:

local plyrs = game:GetService("Players")
local MarketPlaceService = game:GetService("MarketplaceService")

local ongoingProductPurchases = {}
local productIds = {
	[1232493039] = 500, 
	[1232493120] = 1000, 
	[1232493242] = 1500, 
	[1232493507] = 4100, 
	[1232493505] = 11000
}

local function processReceipt(receiptInfo)
	--[[ In your old script, you created new locals for the Players service
		and for the MarketplaceService when you didn't need to, since you have
		access to them from the outer scope above. :) 
		
		local plyrs = game:GetService("Players")
		local MarketPlaceService = game:GetService("MarketplaceService")
	]]
	
	local plr = plyrs:GetPlayerByUserId(receiptInfo.PlayerId)
	local productId = receiptInfo.ProductId
	
	--[[ Not a guarantee this object will exist, so wait for the object for 2 seconds.
		If it doesn't load by that time, instantly reject the purchase. ]]
	local MembershipStatus = plr:WaitForChild("Membershipstatus", 2)
	if not MembershipStatus then 
		print("Not going through.")
		return Enum.ProductPurchaseDecision.NotProcessedYet 
	end
	
	if not plr then
		print("Not going through.")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	if ongoingProductPurchases[plr.UserId] and ongoingProductPurchases[plr.UserId][productId] then
		print("Product purchase is in progress!")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end

	--[[ Same situation here as the Membershipstatus object, we're going to wait for 
	the PlayerValues and the Tokens objects, since they may not exist. ]]
	local playerValues = plr:WaitForChild("PlayerValues", 2)
	if not playerValues then
		print("Not going through.")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	local tokens = playerValues:WaitForChild("Tokens")
	if not tokens then
		print("Not going through.")
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end
	
	--[[ Now lets move the actual assigning of the product purchase in progress here, since
		we've confirmed everything exists that we need! ]]
	if not ongoingProductPurchases[plr.UserId] then
		ongoingProductPurchases[plr.UserId] = {}
	end
	ongoingProductPurchases[plr.UserId][productId] = true
	
	--[[ The error was that you didn't return a purchase granted if the membership
		status was false, hence it was never rejected or granted. :) ]]
	
	--[[ Also a little tip, instead of doing something like:
			
			local value = 100
			value = value + 100
			
		You can actually shorten this using +=, which means the same thing as above, example:
			
			local value = 100
			value += 100	
	]]
	if MembershipStatus.Value == false then
		tokens.Value += productIds[productId]
		print(productIds[productId])
		
		--[[ Now that we know we have a granted purchase, 
			lets set that ongoing purchase to be false again! ]]
		ongoingProductPurchases[plr.UserId][productId] = false
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
	elseif MembershipStatus.Value == true then
		local add = productIds[productId] / 100
		tokens.Value += (add * 115) 
		print(add*115) 
		
		--[[ Now that we know we have a granted purchase, 
			lets set that ongoing purchase to be false again! ]]
		ongoingProductPurchases[plr.UserId][productId] = false
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end

	-- If the code even gets here, we can assume the purchase was not granted.
	ongoingProductPurchases[plr.UserId][productId] = false
	return Enum.ProductPurchaseDecision.NotProcessedYet
end

MarketPlaceService.ProcessReceipt = processReceipt

Hope I’ve helped, let me know if there’s any questions or issues you may have. :+1:

2 Likes

yea sorry for not updating this page I figured it out by myself.The problem was that i didnt put a purchase success enum after the membership == false.Thanks for the help tho

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.