Global leaderboard not working with no errors

Hi there! So I am trying to make a global leaderboard but for some reason it doesnt work and there arent any errors in the output. Here is my code, I’m kind of stuck right now. Also I do have a leaderstats set up with another data store system

local DS = game:GetService("DataStoreService")
local WinsLeaderboard = DS:GetOrderedDataStore("GlobalLeaderboard")
local updateTime = 10

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = WinsLeaderboard:GetSortedAsync(false, 5)
		local WinsPage = Data:GetCurrentPage()
		for rank, data in ipairs(WinsPage) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local name = userName
			local Amount = data.value
			local isOnLeaderboard = false
			for i, v in pairs(game.Workspace.LeaderboardGlobal.LeaderBoardGUI.Holder:GetChildren()) do
				if v.Player.Text == name then
					isOnLeaderboard = true
					break
				end
			end
			
			if Amount and isOnLeaderboard == false then
				local newTemplate = game.ReplicatedStorage:WaitForChild("Template"):Clone()
				newTemplate.Player.Text = name
				newTemplate.Gunpowder.Text = Amount
				newTemplate.Rank.Text = "#"..rank
				newTemplate.Position = UDim2.new(0,0,newTemplate.Position.Y.Scale + (.06 * #game.Workspace.LeaderboardGlobal.LeaderBoardGUI.Holder:GetChildren()), 0)
				newTemplate = game.Workspace.LeaderboardGlobal.LeaderBoardGUI.Holder
			end
		end
	end)
	if not success then
		print(errorMessage)
	end
end

while true do
	for _, player in pairs(game.Players:GetPlayers()) do
		WinsLeaderboard:SetAsync(player.UserId, player.leaderstats.Gunpowder.Value)
	end
	
	for _, frame in pairs(game.Workspace.LeaderboardGlobal.LeaderBoardGUI.Holder:GetChildren()) do
		frame:Destroy()
	end
	
	updateLeaderboard()
	print("Leaderboard Stats Updated")
	wait(updateTime)
end

Can you try putting some prints in to confirm the process flows is as you expect it.
I am asking as there is a lot of code inside the pcall.

I think I might have found the issue. On the last line of your
if Amount and isOnLeaderBoard ... statement you have the following code:

newTemplate = game.Workspace.LeaderboardGlobal.LeaderBoardGUI.Holder

This line removes the reference for your Template object, and instead replaces it with the GUI object Holder.

Try changing it to
newTemplate.Parent = game.Workspace.LeaderboardGlobal.LeaderBoardGUI.Holder

1 Like

Oh my god I am such an idiot thank you so much

No worries, happens to the best of us!

1 Like