Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is this doubling the value?

Asked by 9 years ago

I am trying to make a credit shop, and when they buy the dev. product it adds 10 credits to their value When I buy the product it doubles the amount that I already have (ex. I start with 110, and buy it now I have 220... I should only be getting ten)

Heres the script :

local player = game.Players.LocalPlayer
local cash = game.Players.LocalPlayer.Cash

function CompletePurchase()
    wait(1)
   player.Cash.Value = player.Cash.Value + 10
end

local MarketplaceService = game:GetService("MarketplaceService")
local ds = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
local productId = 23429504


MarketplaceService.ProcessReceipt = function(receiptInfo) 


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


            if receiptInfo.ProductId == productId then

                CompletePurchase()
            end
        end
    end 

    local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId
    ds:IncrementAsync(playerProductKey, 1)  

    return Enum.ProductPurchaseDecision.PurchaseGranted     
end

There is also another script :

local buy = script.Parent
local productId = 23429504

buy.MouseButton1Click:connect(function()
    game:GetService("MarketplaceService"):PromptProductPurchase(game.Players.LocalPlayer, productId)
end)
0
This code shows no reason as to why you would be getting the explained bug, do you have other scripts that are tied to ProcessReceipt? Or is it just a coincidence, like you're naturally gaining 100 right when you buy the Developer Product? Goulstem 8144 — 9y
0
Can you edit your answer to display the script that Prompts the purchase please? Goulstem 8144 — 9y
0
Added. UnleashedGamers 257 — 9y
0
I'm sorry, I have no idea why your code would be reacting this way - there's no reason for it to. But the thing is that i've heard of people having this bug before so it might be an actual bug. You should check and make sure that there's nothing else defining the ProcessReceipt in your game. Goulstem 8144 — 9y
0
Do you have anything cloning either of these scripts, or are they just in a backpack or playergui? If the scripts are getting cloned (specifically, getting cloned every time a purchase occurs), perhaps that might explain why the function is getting run multiple times. chess123mate 5873 — 9y

2 answers

Log in to vote
0
Answered by 9 years ago

Assuming this is part of a leaderboard do this if not give me the script or scripts associated with that cash value.

player.leaderstats.Cash.Value=player.leaderstats.cash.Value+10
0
When I buy it one time it adds 10, which is correct, but if I buy twice it double from 20 to 40. UnleashedGamers 257 — 9y
0
try adding debounce to it threatboy101 2 — 9y
0
please send me the scripts associated with the cash value threatboy101 2 — 9y
0
Sent through a PM UnleashedGamers 257 — 9y
0
Yeah try the line above tell me if it helps. threatboy101 2 — 9y
Ad
Log in to vote
0
Answered by 9 years ago

Note that the API states that you must only set "ProcessReceipt" once and you should do it ServerSide. The function is expected to be able to handle any/all purchases, not just of a specific type.

If you make this change and still have issues, add a record of what receipt IDs have been dealt with and if the current receipt ID is in that dictionary, return immediately. ex:

idsDealtWith = {}
MarketplaceService.ProcessReceipt = function(receiptInfo)
    if idsDealtWith[receiptInfo.PurchaseId] then return Enum.ProductPurchaseDecision.PurchaseGranted end
    idsDealtWith[receiptInfo.PurchaseId] = true
    --rest of code here
end

The API actually recommends this code:

local PurchaseHistory = game:GetService("DataStoreService"):GetDataStore("PurchaseHistory")
MarketplaceService.ProcessReceipt = function(receiptInfo)
    local playerProductKey = receiptInfo.PlayerId .. ":" .. receiptInfo.PurchaseId
    if PurchaseHistory:GetAsync(playerProductKey) then
        return Enum.ProductPurchaseDecision.PurchaseGranted --We already granted it.
    end
    --deal with purchase here
    PurchaseHistory:SetAsync(playerProductKey, true)
    return Enum.ProductPurchaseDecision.PurchaseGranted 
end

Answer this question