If you buy it for the first time, you can buy it normally, but if you buy it for the second time, you can buy it twice. How can I fix it

There is money on the leaderboard. I made it possible to buy money with Lobos

If you buy it for the first time, you can buy it normally, but if you buy it for the second time, you can buy it twice. How can I fix it

example=>100 +100 =200 (I want to make it like this)
Actual error =>100 +100 = 300

local  market = game:GetService("MarketplaceService")
market.ProcessReceipt = function(info)
	local userid = info.PlayerId
	local productid = info.ProductId
	local plr = game.Players:GetPlayerByUserId(userid)
	local leaderboard = plr.leaderstats
	local coins = leaderboard.Money
	
	if productid == 1506484176 then
		coins.Value += 100
	
	elseif productid == 1507002962 then
	   coins.Value += 300
	
	elseif productid == 1507003592 then
	   coins.Value += 700

		
	elseif productid == 1507004130 then
		coins.Value += 1000
	end
		
	
end
2 Likes
local  market = game:GetService("MarketplaceService")
market.ProcessReceipt = function(info)
	local userid = info.PlayerId
	local productid = info.ProductId
	local plr = game.Players:GetPlayerByUserId(userid)
	local leaderboard = plr.leaderstats
	local coins = leaderboard.Money
	
	if productid == 1506484176 then
		coins.Value += 100
	end

	if productid == 1507002962 then
	   coins.Value += 300
	end

	if productid == 1507003592 then
	   coins.Value += 700
    end
	
	if productid == 1507004130 then
		coins.Value += 1000
	end
end

I think I encountered the same problem. I removed all the elseif and it worked for some reason.

I tried it earlier, and it was the same… I tried it again and it’s still the same

This is really a weird thing tbh. I don’t have any idea…

Maybe try

coins.Value = coins.Value + [placeholder]

?

[Placeholder] How do I use this It is recognized as an error when writing

It’s a placeholder, replace it with whatever you want since there’s multiple instances of this line in your code

Save last value from BEFORE purchased, then check the value after, if its different bigger than the placeholder then its supposed to be take it away. Idk this is my solution, not sure if it will work. Example:

if productid == [ID] then
local oldCash = coins.Value
wait(1)
coins.Value += [placeholder]
if oldCash == coins.Value + 300 then
coins.Value = coins.Value - 100
end
end

I know it’s a lot just for 1 but it might work

I researched a bit. Basically what happens is Roblox wants to know if purchase is successful. Until it is told, this callback will be running many times. Therefore, it runs two times because it thinks that the purchase isn’t successful. But when it becomes successful, it already runs two times. Here’s some links to reference. ProcessReceipt and ProductPurchaseDecision .

local function processReceipt(receipt)
    --do stuff
    return Enum.ProductPurchaseDecision.PurchaseGranted
end

marketPlaceService.ProcessReceipt:Connect(processReceipt)

For your code, this would be -

local  market = game:GetService("MarketplaceService")
market.ProcessReceipt = function(info)
	local userid = info.PlayerId
	local productid = info.ProductId
	local plr = game.Players:GetPlayerByUserId(userid)
	local leaderboard = plr.leaderstats
	local coins = leaderboard.Money
	
	if productid == 1506484176 then
		coins.Value += 100
	
	elseif productid == 1507002962 then
	   coins.Value += 300
	
	elseif productid == 1507003592 then
	   coins.Value += 700

	elseif productid == 1507004130 then
		coins.Value += 1000
	end

return Enum.ProductPurchaseDecision.PurchaseGranted
end
1 Like

Thank you very much, you’re a genius, it works perfectly

1 Like

Please mark it as a solution. And NP. Always there to help :slight_smile:

Tip - Use pcall() to see if purchase is granted or not.

Just a tip: you should structure your dev product stem like this:

local DeveloperProducts = {
    [devProductIdHere] = function()
        Cash.Value += amountToGive
    end
    --keep adding products
}
1 Like

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