Why doesn't my leaderboard work?

It prints that the leaderboard has been updated, but nothing shows up on the part, there are no errors that are printed.

local DS = game:GetService("DataStoreService")
local DataStore = DS:GetOrderedDataStore("Donate")

local function SetLeaderboard()
	local yes, no = pcall(function()
		local data = DataStore:GetSortedAsync(false, 5)
		local page = data:GetCurrentPage()
		for rank, data in ipairs(page) do
			local Name = game.Players:GetPlayerByUserId(tonumber(data.key))
			local Robux = data.Value
			local onLeaderboard = false
			for _, v in pairs(game.Workspace.Leaderboard.SurfaceGui.Display:GetChildren()) do
				if v.Player.Text == Name then
					onLeaderboard = true
					break
				end
			end
			
			if Robux and not onLeaderboard then
				local Frame = game.ReplicatedStorage.Template:Clone()
				Frame.Name.Text = Name
				Frame.Amount.Text = Robux
				Frame.Place.Text = "#"..rank
				Frame.Position = UDim2.new(0,0,0.1 * #game.Workspace.Leaderboard.SurfaceGui.Display:GetChildren(),0)
				Frame.Parent = game.Workspace.Leaderboard.SurfaceGui.Display
			end
		end
	end)
	
	if not yes then
		warn(no)
	end
end

while true do
	for _, v in pairs(game.Players:GetPlayers()) do
		if v.Robux.Value > 0 then
			DataStore:SetAsync(v.UserId,v.Robux.Value)
		end
	end
	for _, v in pairs(game.Workspace.Leaderboard.SurfaceGui.Display:GetChildren()) do
		v:Destroy()
	end
	SetLeaderboard()
	print("Updated Leaderboard")
	wait(60)
end

I don’t see anything that would completely stop the leaderboard from not showing, but I see a few issues:

first, one issue I see that you’re using (GetPlayerFromUserId)[Players | Documentation - Roblox Creator Hub], this gets the player instance from a userId if the player is ingame, it’s nil otherwise, so it should give you problems like the inner for loop never working, and setting .Text to nil

second, the for loop on the bottom assumes each player instance has .Robux, it’s not a major issue, but you should use FindFirstChild and make sure it exists before using it

you should add prints when iterating through the current page, maybe it’s an empty page and nothing is showing because there’s no data
then, also output when saving data, maybe there’s small issues like everyone’s Robux is 0, so no one is being saved
there may have also been an issue while the script was loading as the game starts, that may have been flooded out by other output

It doesn’t seem as if you’re making the “Frame” visible.

Ah ok, thank you for the info!

just realized it was because I capitlized V in Value too, its supposed to be value lol

1 Like