I just started trying out Developer products today… and it’s kinda throwing me off, along with the lack of details in documentation on for it on the wiki.
So far I am trying to make a counter of how many times a product has been purchased.
What I am getting is the counter adding up even when I don’t click the button to purchase anything.
My code so far:
local MarketplaceService = game:GetService(“MarketplaceService”)
local testID = 19352390
local PurchaseHistory = game:GetService(“DataStoreService”):GetDataStore(“PurchaseHistory”)
local DataStore = game:GetService(“DataStoreService”):GetDataStore(“TimesBought”)
local PStore = game:GetService(“DataStoreService”):GetDataStore(“PurchaseID”)
MarketplaceService.ProcessReceipt = function(receiptInfo)
for i, player in ipairs(game.Players:GetChildren()) do
if player.userId == receiptInfo.PlayerId then
local key = “user_” … player.userId
local previousId = PStore:GetAsync(key) or “”
if receiptInfo.ProductId == testID then
if previousId == "" or recieptInfo.PurchaseId ~= previousId then
player.PlayerGui.ScreenGui.Status.Text = "Test1 Bought"
DataStore:UpdateAsync(key, function(oldValue)
local newValue = oldValue or 0 --oldValue might be nil
newValue = newValue + 1
player.PlayerGui.ScreenGui.Times.Text = "Times Bought : " ..newValue
return newValue
end)
PStore:UpdateAsync(key, function(oldValue)
local newValue = oldValue or "" --oldValue might be nil
newValue = recieptInfo.PurchaseId
return newValue
end)
player.PlayerGui.ScreenGui.ID.Text = receiptInfo.PurchaseId
end
end
end
end
-- record the transaction in a Data Store
local playerProductKey = "player_" .. receiptInfo.PlayerId .. "_purchase_" .. receiptInfo.PurchaseId
PurchaseHistory:IncrementAsync(playerProductKey, 1)
return Enum.ProductPurchaseDecision.PurchaseGranted
end
Sorry the code here doesn’t indent, otherwise it would be easier to understand