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
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
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.