Top Donators list updated even though i pressed cancel on the Purchase Prompt?

Hello everyone!

I’m currently making a Donation Leaderboard for my game, at first glance the Leaderboard works just fine, the Effects play when you donate, it plays a sound, & updates the leaderboard.

but when i cancel the the Purchase Prompt, it still updates the leaderboard… what is going on & how do i fix it? thanks :smile:

Donation Sent Script
local marketPlaceService = game:GetService("MarketplaceService")
local donationModule = require(script.Parent)

local function onPrompt(plrId, id, isPurchased)

	local productInfo = marketPlaceService:GetProductInfo(id, Enum.InfoType.Product)
	local price = productInfo.PriceInRobux
	local plr = game.Players:GetPlayerByUserId(plrId)

	donationModule.updtLeaderboard(plr, price)
end

marketPlaceService.PromptProductPurchaseFinished:Connect(onPrompt)
Donation Module
--# data store
local dataStoreService = game:GetService("DataStoreService")
local donationStore = dataStoreService:GetOrderedDataStore("Donation")

local module = {}

--# cleans up the leaderboard
local function cleanUp_board(plr)
	for i, dono in pairs(plr.PlayerGui.GUI.MainGUI.DonateGui.Background.Leaderboard:GetChildren()) do
		if dono:IsA("Frame") then
			dono:Destroy()
		end
	end

	module.SetUp_Board()
end

--# sets up the leaderboard
function module.SetUp_Board()
	local isAscending = false
	local pageSize = 10
	local pages = donationStore:GetSortedAsync(isAscending, pageSize)
	local topTen = pages:GetCurrentPage()

	for rank, data in ipairs(topTen) do
		local plrId = data.key
		local amount = data.value

		local plr = game.Players:GetPlayerByUserId(plrId)
		local usrName = plr.Name

		if not usrName then continue end

		local thumbnailType, thumbnailSize = Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420

		local avatarImg = game.Players:GetUserThumbnailAsync(plrId, thumbnailType, thumbnailSize)

		local donoFrame = script.DonoFrame:Clone()
		donoFrame.Parent = plr.PlayerGui.GUI.MainGUI.DonateGui.Background:WaitForChild("Leaderboard")

		donoFrame.LayoutOrder = rank
		donoFrame.Username.Text = usrName

		donoFrame.PlrAvatar.Image = avatarImg
		donoFrame.Rank.Text = rank
		donoFrame.RBX_Amount.Text	 = amount
	end
end

--# updates the leaderboard
function module.updtLeaderboard(plr, amount)
	local usrId = plr.UserId
	local success, currentAmount = pcall(function()
		return donationStore:GetAsync(usrId)
	end)

	if currentAmount then
		currentAmount += amount
	else
		currentAmount = amount
	end

	donationStore:SetAsync(usrId, currentAmount)

	cleanUp_board(plr)
end

return module

Make sure to confirm if the product has actually been purchased once the prompt has concluded.

local function onPrompt(plrId, id, isPurchased)

	local productInfo = marketPlaceService:GetProductInfo(id, Enum.InfoType.Product)
	local price = productInfo.PriceInRobux
	local plr = game.Players:GetPlayerByUserId(plrId)

    if isPurchased then
	   donationModule.updtLeaderboard(plr, price)
    end
end

I also recommend using pcall() when working with MarketplaceService.

2 Likes

oh my god… thats it? :skull: thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.