Leaderboard DataStore problem

Hello, I have a global leaderboard for my game that shows people that have the most number of leaderstat called “Drops”, next to the leaderboard are 3 statues and on them are displayed top 3 players with an overhead gui that’s displaying their stats (place, name, drops), everything was working really good, but then I reseted leaderstat “Drops” and OrderDataStore of the leaderboard for top 3 players because I wanted to remove them, it did reset but an issue appeared, the overhead gui stopped appearing above the statues heads. At first top 2 players didn’t have their overhead gui displayed and the third player had an overhead gui above his statue, when he got to top 2 his gui was still showing and the player that went from second to third stil didn’t have his gui until that player that got to second place changed his avatar, it stopped working for him too, also when I play test the leaderboard refreshes on server start and in the refresh the gui appears showing correct stats for each player and when the refresh is done the gui’s just disappear, I don’t want happend I just rested the stat and didn’t touch the leaderboard and it just stopped showing the gui’s, if anybody knows how to fix this I would appreciate the help, here is the full main script for the leaderboard:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")

local URL = "https://users.roproxy.com/v1/users/"
local AbbreviateModule = require(script.Parent:FindFirstChild("Abbreviate"))
local DropsDS = DataStoreService:GetOrderedDataStore("DropsLeaderboard")

local Leaderboard = script.Parent
local LeaderboardPart = Leaderboard:FindFirstChild("LeaderboardPart")
local LeaderboardGui = LeaderboardPart:FindFirstChild("LeaderboardGui")
local PlayerHolder = LeaderboardGui:FindFirstChild("FrameHolder"):FindFirstChild("PlayerHolder")

local Statues = Leaderboard:FindFirstChild("Statues")
local GoldStatue = Statues:FindFirstChild("Gold"):FindFirstChild("Character")
local SilverStatue = Statues:FindFirstChild("Silver"):FindFirstChild("Character")
local BronzeStatue = Statues:FindFirstChild("Bronze"):FindFirstChild("Character")

local Sample = script:FindFirstChild("Sample")
local RefreshTime = 60
local OWNER_USERNAME = "Bobek858"

-- Format numbers with commas
local function commaValue(n)
	local left, num, right = tostring(n):match('^([^%d]*%d)(%d*)(.-)$')
	return left .. (num:reverse():gsub("(%d%d%d)", "%1,"):reverse()) .. right
end

-- Rainbow effect
local function rainbowText(label)
	task.spawn(function()
		while label.Parent do
			label.TextColor3 = Color3.new(255/255, 0/255, 0/255)

			for i = 0, 255, 10 do
				task.wait()
				label.TextColor3 = Color3.new(255/255, i/255, 0/255)
			end
			for i = 255, 0, -10 do
				task.wait()
				label.TextColor3 = Color3.new(i/255, 255/255, 0/255)
			end
			for i = 0, 255, 10 do
				task.wait()
				label.TextColor3 = Color3.new(0/255, 255/255, i/255)
			end
			for i = 255, 0, -10 do
				task.wait()
				label.TextColor3 = Color3.new(0/255, i/255, 255/255)
			end
			for i = 0, 255, 10 do
				task.wait()
				label.TextColor3 = Color3.new(i/255, 0/255, 255/255)
			end
			for i = 255, 0, -10 do
				task.wait()
				label.TextColor3 = Color3.new(255/255, 0/255, i/255)
			end
		end
	end)
end

-- Apply player appearance to statue
local function setRig(Rig, UserId)
	task.spawn(function()
		local success, playerDesc = pcall(function()
			return Players:GetHumanoidDescriptionFromUserId(UserId)
		end)
		if success then
			local RigHumanoid = Rig:FindFirstChild("Humanoid")
			if RigHumanoid then
				local EmptyDesc = Instance.new("HumanoidDescription")
				RigHumanoid:ApplyDescription(EmptyDesc)
				task.wait(0.1)
				RigHumanoid:ApplyDescription(playerDesc)
			end
		end
	end)
end

-- Save player drops
local function savePlayerDrops(player)
	if player.Name == OWNER_USERNAME then return end
	if player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Drops") then
		local success, err = pcall(function()
			DropsDS:SetAsync(player.UserId, player.leaderstats.Drops.Value)
		end)
		if not success then
			warn("Error saving drops for " .. player.Name .. ": " .. tostring(err))
		end
	end
end

-- Update all players' overhead place labels
local function updateOverheadPlaces()
	local success, data = pcall(function()
		return DropsDS:GetSortedAsync(false, 3)
	end)
	if success then
		local top3Ids = {}
		for _, entry in ipairs(data:GetCurrentPage()) do
			local userId = tonumber(entry.key)
			if userId then
				local okName, username = pcall(function()
					return Players:GetNameFromUserIdAsync(userId)
				end)
				if okName and username ~= OWNER_USERNAME then
					table.insert(top3Ids, userId)
				end
			end
		end

		for _, player in ipairs(Players:GetPlayers()) do
			if player.Character and player.Character:FindFirstChild("Head") then
				local head = player.Character.Head

				-- Ensure OverheadGui exists (wait if not yet replicated)
				local overhead
				local timeout = 5
				local startTime = tick()
				repeat
					overhead = head:FindFirstChild("OverheadGui")
					if overhead then break end
					task.wait(0.1)
				until tick() - startTime >= timeout

				if overhead then
					local frame = overhead:FindFirstChild("Frame")
					if frame then
						local placeLabel = frame:FindFirstChild("PlaceLabel")
						if placeLabel then
							local rank = table.find(top3Ids, player.UserId)
							if rank then
								placeLabel.Text = "#" .. tostring(rank)
								rainbowText(placeLabel)
							else
								placeLabel.Text = ""
								placeLabel.TextColor3 = Color3.new(1, 1, 1)
							end
						end
					end
				end
			end
		end
	else
		warn("Failed to get top 3 players for overhead update")
	end
end

-- Refresh leaderboard
local function refreshLeaderboard()
	for _, Frame in ipairs(PlayerHolder:GetChildren()) do
		if Frame:IsA("Frame") then
			Frame:Destroy()
		end
	end

	for _, player in ipairs(Players:GetPlayers()) do
		pcall(savePlayerDrops, player)
	end

	local success, err = pcall(function()
		local data = DropsDS:GetSortedAsync(false, 10)
		local dataPage = data:GetCurrentPage()
		local displayRank = 1

		for _, savedData in ipairs(dataPage) do
			local userId = tonumber(savedData.key)
			local drops = savedData.value
			local username = "Unknown"
			local displayName = "Unknown"
			local thumb = ""

			local ok1, result1 = pcall(function()
				return Players:GetNameFromUserIdAsync(userId)
			end)
			if ok1 and result1 then
				username = result1
			end
			if username == OWNER_USERNAME then
				continue
			end

			local ok2, result2 = pcall(function()
				local playerData = HttpService:GetAsync(URL .. userId)
				return HttpService:JSONDecode(playerData)
			end)
			if ok2 and result2 and result2.displayName then
				displayName = result2.displayName
			else
				displayName = username
			end

			local ok3, content = pcall(function()
				return Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size60x60)
			end)
			if ok3 and content then
				thumb = content
			end

			-- Create leaderboard entry
			local newSample = Sample:Clone()
			newSample.Parent = PlayerHolder
			newSample.Name = username

			local prof = newSample:FindFirstChild("ProfilePicture")
			local nameLbl = newSample:FindFirstChild("Name")
			local displayLbl = newSample:FindFirstChild("DisplayName")
			local winsLbl = newSample:FindFirstChild("Wins")
			local placeLbl = newSample:FindFirstChild("Place")

			if prof then prof.Image = thumb end
			if nameLbl then nameLbl.Text = "@" .. username end
			if displayLbl then displayLbl.Text = displayName end
			if winsLbl then winsLbl.Text = commaValue(drops) end
			if placeLbl then
				placeLbl.Text = "#" .. tostring(displayRank)
				if displayRank <= 3 then
					rainbowText(placeLbl)
				else
					placeLbl.TextColor3 = Color3.new(1, 1, 1)
				end
			end

			-- Update statues
			local function setOverhead(statue)
				local overheadGui = statue:FindFirstChild("Head"):FindFirstChild("OverheadGui")
				if overheadGui then
					local frame = overheadGui:FindFirstChild("Frame")
					if frame then
						local dropsLabel = frame:FindFirstChild("DropsLabel")
						local usernameLabel = frame:FindFirstChild("UsernameLabel")
						if dropsLabel then
							dropsLabel.Text = commaValue(drops)
							rainbowText(dropsLabel)
						end
						if usernameLabel then
							usernameLabel.Text = username
						end
					end
				end
			end

			if displayRank == 1 then
				setRig(GoldStatue, userId)
				setOverhead(GoldStatue)
			elseif displayRank == 2 then
				setRig(SilverStatue, userId)
				setOverhead(SilverStatue)
			elseif displayRank == 3 then
				setRig(BronzeStatue, userId)
				setOverhead(BronzeStatue)
			end

			displayRank += 1
		end
	end)

	if not success then
		warn("Error refreshing leaderboard: " .. tostring(err))
	end
end

Players.PlayerRemoving:Connect(savePlayerDrops)

-- Main loop
while true do
	refreshLeaderboard()
	task.wait(1) -- wait 1 second after leaderboard refresh
	updateOverheadPlaces()
	task.wait(RefreshTime - 1)
end

Thanks in advance!

2 Likes

Anyone knows how to fix this issue?

Please provide the error messages.

1 Like

There is no errors in the output.