Updating Leaderstats bug

I have some code that accesses a players t shirts (basically like pls donate) and it should add the amount that the t shirt cost to their leaderstat value called ‘Donated’ however, sometimes it works perfectly and adds the correct amount, and sometimes it doesn’t, it seems completely random. For example I donated something worth $4 and it added $17 to my leaderstats value. I also donated $20 and it added 100k, any ideas why this is? I’ll add the code below, sorry its quite long.

The part where I detect payment

local function donateButtonClicked(donateButton)

						local itemId = donateButton.Value.Value
						local itemPrice = donateButton.Price.Value

						MarketPlaceService:PromptPurchase(loserPlayer, itemId)

						MarketPlaceService.PromptPurchaseFinished:Connect(function(loserPlayer, itemId, was_purchased)

							if was_purchased then

								loserPlayer.leaderstats.Donated.Value += itemPrice
								local playerId = loserPlayer.UserId
								
							else
								return
							end
							return
						end)
						return
					end

Whole code

local function FormatUserGeneratedTShirtsEndpoint(UserId, Cursor)
						Cursor = Cursor or ""
						return "https://catalog.roproxy.com/v1/search/items/details?Category=Clothing&Subcategory=ClassicTShirts&Limit=30&CreatorTargetId="..UserId.."&Cursor="..Cursor
					end
					local function TableConcat(TableA, TableB)
						local TableC = {}
						for i, v in ipairs(TableA) do
							table.insert(TableC, v)
						end
						for i, v in ipairs(TableB) do
							table.insert(TableC, v)
						end
						return TableC
					end
					local function GetUserGeneratedTShirts(UserId, Limit, Cursor)
						local TShirts = {}
						local Endpoint = FormatUserGeneratedTShirtsEndpoint(UserId, Cursor)
						local Success, Result = pcall(function()
							return Http:GetAsync(Endpoint)
						end)
						if not Success then
							warn("Unable to get t-shirts from API: "..Result)
							return TShirts
						end
						local Success2, Result2 = pcall(function()
							return Http:JSONDecode(Result)
						end)
						if not Success2 then
							warn("Unable to decode response from t-shirts API: "..Result2)
							return TShirts
						end
						for _, TShirt in ipairs(Result2.data) do
							table.insert(TShirts, TShirt.id)
						end
						if #TShirts >= Limit then
							return TShirts
						else
							Cursor = Result2.nextPageCursor
							if Cursor then
								local MoreTShirts = GetUserGeneratedTShirts(UserId, Limit, Cursor)
								return TableConcat(TShirts, MoreTShirts)
							else
								return TShirts
							end
						end
					end
					
					local userTShirts = GetUserGeneratedTShirts(winnerId, 10)
					local num = (#userTShirts)
					
					loser.PlayerGui.Donation.Frame.TextLabel.Text = "Donate to ".. winnerName
					
					local table1 = {}
					
					for i = num, 1, -1 do
						local Asset = MarketPlaceService:GetProductInfo(userTShirts[i])
						local assetPrice = Asset.PriceInRobux
						local assetId = Asset.AssetId
						
						if assetPrice == nil or assetId == nil then
							print("Failure")
						else 
							-- Create a new table for each item with the price and id
							local item = {price = assetPrice, id = assetId}

							-- Insert the item into the table
							table.insert(table1, item)
						end

						-- Create a new table for each item with the price and id
						
					end
					
					table.sort(table1, function(a, b)
						return a.price < b.price
					end)

					for i, item in ipairs(table1) do
						if item then
							local price = item.price or "default price"
							local id = item.id or "unknown"
							
							local newButton = workspace.DonateButton:Clone()
							newButton.Name = "DonateButton"..i
							local priceValue = Instance.new("IntValue", newButton)
							priceValue.Name = "Price"
							priceValue.Value = price
							newButton.Text = price .. " R$" 

							local id = Instance.new("IntValue", newButton)
							id.Value = item.id

							newButton.Parent = loser.PlayerGui.Donation.Frame.itemsScroller
							
						else
							
						end
					end
					
					local function donateButtonClicked(donateButton)

						local itemId = donateButton.Value.Value
						local itemPrice = donateButton.Price.Value

						MarketPlaceService:PromptPurchase(loserPlayer, itemId)

						MarketPlaceService.PromptPurchaseFinished:Connect(function(loserPlayer, itemId, was_purchased)

							if was_purchased then

								loserPlayer.leaderstats.Donated.Value += itemPrice
								local playerId = loserPlayer.UserId
								
							else
								return
							end
							return
						end)
						return
					end

					for i,v in pairs(loserPlayer.PlayerGui.Donation.Frame.itemsScroller:GetChildren()) do
						if not (v:IsA("TextButton") or v:IsA("ImageButton")) then continue end
						v.MouseButton1Click:Connect(function()
							donateButtonClicked(v)
						end)
					end

One thing I saw is the PromptPurchaseFinished connection should not be inside the donateButtonClicked function, it should be outside the function.

Every time you click the button, right now it connects the code that adds the amount to the leaderstat, which stacks. Which could lead to incorrect amounts being added after clicking the button multiple times.

Just take it out of the donateButtonClicked function and put it above it. I didn’t see anything else wrong on first look, but I’ll continue to look for anything else that might need changed.

1 Like

Amazing thanks, I’ll try that out now, thanks for your help :slight_smile:

Sorry for the very late update, but it’s still not working.

Here is the updated script:

MarketPlaceService.PromptPurchaseFinished:Connect(function(loserPlayer, itemId, was_purchased)

						if was_purchased then
							
							local productInfo = MarketPlaceService:GetProductInfo(itemId, Enum.InfoType.Product)
							local itemPrice = productInfo.PriceInRobux

							loserPlayer.leaderstats.Donated.Value += itemPrice
							local playerId = loserPlayer.UserId

						else
							return
						end
						return
					end)
					
					local function donateButtonClicked(donateButton)

						local itemId = donateButton.Value.Value
						local itemPrice = donateButton.Price.Value

						MarketPlaceService:PromptPurchase(loserPlayer, itemId, itemPrice)

						return
					end

					for i,v in pairs(loserPlayer.PlayerGui.Donation.Frame.itemsScroller:GetChildren()) do
						if not (v:IsA("TextButton") or v:IsA("ImageButton")) then continue end
						v.MouseButton1Click:Connect(function()
							donateButtonClicked(v)
						end)
					end