DevProduct adding too much

Hi, sorry to bother again.

This script is supposed to add a certain amount of money (bucks) to the player’s stats when they buy the dev product.
The amount of bucks to add is determined by the “amount” stated below.

However, the game adds wayyy to much bucks.

E.g, when I bought 200 bux, it gave me 1700 bucks instead, then 2800 then 4100 bucks. It keeps changing every time.

Script
function promptPurchase(productID, amount)
	local player = Players.LocalPlayer
	MarketplaceService:PromptProductPurchase(player, productID)
	MarketplaceService.PromptProductPurchaseFinished:Connect(function (...)
		print("PromptProductPurchaseFinished", ...)
		player.leaderstats.Bucks.Value = player.leaderstats.Bucks.Value + amount
	end)
end

for i, mage in pairs(folder:GetDescendants()) do
	if mage:IsA("ImageButton") then
		local productID = mage.Product.Value
		local amount = mage.Amount.Value
		mage.MouseButton1Click:Connect(function()
			promptPurchase(productID, amount)
		end)
	end
end

Any help?

I’m pretty sure you’re supposed to return some sort of MarketplaceTransactionFinished thing somewhere or it will keep retrying until you do.

The most notable functions are PromptProductPurchase and PromptPurchase, as well as the callback ProcessReceipt which must be well defined so that transactions do not fail.

Did you set up the ProcessReceipt callback? Can you include the code here so that we can assist you further?

Ah. I did not include the ProcessReceipt.

Any chance you could help me out with that?

This happens for two reasons, the first one as @T0ny said is because you aren’t returning Enum.ProductPurchaseDecision.PurchaseGranted after the currency is added. The second reason is because every time promptPurchase runs it connects a new event listening for PromptProductPurchaseFinished(also the above script wont work anyway because it’s a LocalScript and the change wont replicate to the server). Instead a new Script(running on the server) should listen for purchases using ProcessReceipt and return a ProductPurchaseDecision depending on if the purchase was successful or no(basically handling situations where the code may break):

--Script, ServerScriptService
local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")

--[product id] = bucks
local Products = {
	[12345678] = 1000
}

MarketplaceService.ProcessReceipt = function(receiptInfo)
	--pcall in case it fails
	local Success, Error = pcall(function()
		local player = Players:GetPlayerByUserId(receiptInfo.PlayerId)
		local amount = Products[receiptInfo.ProductId]
		player.leaderstats.Bucks.Value += amount or 0 --if the product isn't part of the dictionary, we ignore it.
	end)

	if Success then 
		return Enum.ProductPurchaseDecision.PurchaseGranted	
	else 
		warn(Error)
		return Enum.ProductPurchaseDecision.NotProcessedYet
	end 
end

And on your client script just do:

mage.MouseButton1Click:Connect(function()
	MarketplaceService:PromptProductPurchase(Players.LocalPlayer, productID)
end)

Instead of using your promptPurchase function.

1 Like

You should be prompting the purchase for a product ID. You shouldn’t pass the amount the player should get to a function because the product itself should be providing the player with that amount. Or in other words that amount should be hardcoded.

You shouldn’t connect to PromptProductPurchaseFinished because that event is just for when the prompt closes, it doesn’t mean a purchase was successful. To add on to what @NyrionDev said, only one script in the server can set itself as a callback to ProcessReceipt and that function should handle every product you’re offering.

2 Likes

What do you mean by this? I thought I had to manually give the amount via the function?

Thanks a lot! I now understand what went wrong and how I should fix it. Cheers buddy!