I’m making a donation board and i made leaderstats to track how much a player has donated and a data store to save the data. There is just one problem, when you buy the donation it works fine at first, but when you buy any of them again it says that you donated more than you actually did in the leaderstats. The donating part is fine it’s just i don’t want people to think they donated more than they actually did. I think it has something to do with this code right here.
local service = game:GetService("MarketplaceService")
local function Receipt(info)
local player = game.Players:GetPlayerByUserId(info.PlayerId)
if not player then
return Enum.ProductPurchaseDecision.NotProcessedYet
else
if info.ProductId == 1007006753 then
player.leaderstats["Amount of Robux Donated"].Value = player.leaderstats["Amount of Robux Donated"].Value + 10
elseif info.ProductId == 1010592064 then
player.leaderstats["Amount of Robux Donated"].Value = player.leaderstats["Amount of Robux Donated"].Value + 50
elseif info.ProductId == 1010592314 then
player.leaderstats["Amount of Robux Donated"].Value = player.leaderstats["Amount of Robux Donated"].Value + 100
elseif info.ProductId == 1010593248 then
player.leaderstats["Amount of Robux Donated"].Value = player.leaderstats["Amount of Robux Donated"].Value + 150
elseif info.ProductId == 1010592723 then
player.leaderstats["Amount of Robux Donated"].Value = player.leaderstats["Amount of Robux Donated"].Value + 200
elseif info.ProductId == 1010593691 then
player.leaderstats["Amount of Robux Donated"].Value = player.leaderstats["Amount of Robux Donated"].Value + 250
elseif info.ProductId == 1010594219 then
player.leaderstats["Amount of Robux Donated"].Value = player.leaderstats["Amount of Robux Donated"].Value + 300
end
end
end
service.ProcessReceipt = Receipt
From the looks of it, the code should have no issue. But let me make sure i’m understanding this right. Your [“Amount of Robux Donated”] leaderstat is displaying the TOTAL amount donated, NOT the MOST RECENT donation.
As an example, let’s say ["Amount of Robux Donated"] = 0 at first. Then someone donates 10. The value should show 10. Then the same person donates 50. It should be updated to 60 since they have donated 10 and 50, so 10 + 50. I would like to see an example of how it’s displaying more than they donated. The problem may be that the function is getting fired more than once (how It would I don’t know). Using print statements are handy at debugging this sort of thing.
I think you should use a table to hold the devproducts.
local products = {
[1007006753] = 10;
[1010592064] = 50;
[1010592314] = 100;
[1010593248] = 150;
[1010592723] = 200;
[1010593691] = 250;
[1010594219] = 300;
}
local marketplaceService = game:GetService("MarketplaceService")
local players = game:GetService("Players")
function processReceipt(receiptInfo)
local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
local amountSpent = products[receiptInfo.ProductId] or receiptInfo.CurrencySpent
if not player or not player:FindFirstChild("leaderstats") then
return Enum.ProductPurchaseDecision.NotPurchasedYet
end
player.leaderstats["Amount of Robux Donated"].Value += amountSpent
return Enum.ProductPurchaseDecision.PurchaseGranted
end
marketplaceService.ProcessReceipt = processReceipt
Edit: after writing my script, I realized that you don’t even need a table, as the CurrencySpent index of the Receipt Info table already indicates the amount that the player has spent on the developer-prodcut.
function processReceipt(receiptInfo)
local player = players:GetPlayerByUserId(receiptInfo.PlayerId)
local amountSpent = receiptInfo.CurrencySpent or 0
if not player or not player:FindFirstChild("leaderstats") then
return Enum.ProductPurchaseDecision.NotPurchasedYet
end
player.leaderstats["Amount of Robux Donated"].Value += amountSpent
return Enum.ProductPurchaseDecision.PurchaseGranted
end
For more information, please check out the official API documentation.