Why is this Players data not being updated?

So i’m trying to make a Global Donation leaderboard work. Everything works apart from one for loop where it updates the players data to the ordered datastore. It will update it when the player leaves the game but not in the while loop that updates the leaderboard. No errors in the output. Can anyone tell me what’s wrong. Problem is at bottom while loop of script

local StarterGui = game:GetService("StarterGui")
local DataStoreService = game:GetService("DataStoreService")
local DonationLeaderboard = DataStoreService:GetOrderedDataStore("Donationleaderboard")
local Players = game:GetService("Players")

local products = {
	{
		["Id"] = 1705593449,
		["Price"] = 10
	},
	{
		["Id"] = 1705594693,
		["Price"] = 100
	},
	{
		["Id"] = 1705595838,
		["Price"] = 100000
	},
	{
		["Id"] = 1705595734,
		["Price"] = 10000
	},
	{
		["Id"] = 1705595371,
		["Price"] = 1000
	},
	{
		["Id"] = 1705593593,
		["Price"] = 25
	},
	{
		["Id"] = 1705594960,
		["Price"] = 250
	},
	{
		["Id"] = 1705594418,
		["Price"] = 50
	},
	{
		["Id"] = 1705595079,
		["Price"] = 500
	},
	{
		["Id"] = 1705595660,
		["Price"] = 5000
	},
	{
		["Id"] = 1705594530,
		["Price"] = 75
	},
	{
		["Id"] = 1705595241,
		["Price"] = 750
	} 
}
local leaderboard = game.StarterGui.GlobalDonationLeaderboard

local function leaderboardSetUp()
	for i, product in pairs(products) do 
		local newButton = leaderboard.Donate.Template:Clone()
		newButton.Visible = true
		newButton.Id.Value = product.Id
		newButton.Text = tostring(product.Price).." Robux"
		newButton.Parent = leaderboard.Donate
		newButton.Name = tostring(product.Price)
		newButton.LayoutOrder = product.Price
	end
end
local function updateLeaderBoard()
	local success, errormessage = pcall(function()
		local Data = DonationLeaderboard:GetSortedAsync(false, 50, 1)
		local Page = Data:GetCurrentPage()
		for rank, data in ipairs(Page) do 
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local name = userName
			local Donated = data.value
			local isonLeaderboard = false

			if Donated and isonLeaderboard == false then 
				local newFrame = leaderboard.ScrollingFrame:FindFirstChild("Template"):Clone()
				newFrame.UserName.Text = name
				newFrame.Robux.Text = Donated
				newFrame.Rank.Text = "#"..rank
				newFrame.Parent = leaderboard.ScrollingFrame
				newFrame.LayoutOrder = rank
				newFrame.Visible = true
				newFrame.Name = name
			end
		end
	end)

	if not success then 
		print(errormessage)
	end
end
Players.PlayerRemoving:Connect(function(player)
	DonationLeaderboard:SetAsync(player.UserId, player.Donated.Value)
end)

leaderboardSetUp()
while true do 
	for i, player in pairs(Players:GetPlayers()) do 
			DonationLeaderboard:SetAsync(player.UserId, player.Donated.Value)
			print("Updated "..player.Name)
	end
	for i, frame in pairs(leaderboard.ScrollingFrame:GetChildren()) do 
		if frame:IsA("Frame") then 
			if frame.Name ~= "Template" then 
				frame:Destroy()
			end
		end
	end
	updateLeaderBoard()
	task.wait(60)
end

1 Like

What about the script that changes the “Donated” stat value of players that bought a product?

2 Likes

So the updateLeaderBoard() function is identical when the player exits the server and when it just runs on the 60 second timer you have setup?

If you are always setting the isonLeaderboard value to false, why do you need it for a comparison the next line down since it will always be false no matter what? Not that it would break anything, but probably not necessary.

My guess is, since you get no errors, either the data is not being loaded to sort, so the loop has nothing to do or the local variables inside the loop that are being created over and over, need to be moved outside of the for loop.

That worked perfectly fine. I fixed it because I had the surface GUI in starterGUI so I could code the buttons on there but I had to seperate the leaderboard part back into the workspace so it would actually update because otherwise it would just update when you died. @knightmb1 cheers for the help I was going to use isonLeaderboard but didn’t end up so i should probably get rid of it.

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