Why isn't my leader board working?

So in my game I have two global leaerboards that show the top players for two different stats. The problem is one of the leaderboards works while the other doesn’t. I looked through the output and tried to debug it myself but couldn’t please help.
Notes

  • Gems is the name of the currency

GLOBAL LEADERBOARD

local ds = game:GetService("DataStoreService")

local coinsODS = ds:GetOrderedDataStore("GemsStats")


local timeUntilReset = 10


while wait(1) do
	
	
	timeUntilReset = timeUntilReset - 1
	
	script.Parent.Parent.ResetTime.Text = "Resetting in " .. timeUntilReset .. " seconds..."
	
	
	if timeUntilReset == 0 then
		
		timeUntilReset = 10
	
	
		for i, plr in pairs(game.Players:GetPlayers()) do
			
			coinsODS:SetAsync(plr.UserId, plr.leaderstats.Gems.Value)
		end
		
		for i, leaderboardRank in pairs(script.Parent:GetChildren()) do
			
			if leaderboardRank.ClassName == "Frame" then
				leaderboardRank:Destroy()
			end
		end
		
		
		local success, errorMsg = pcall(function()
			
			local data = coinsODS:GetSortedAsync(false, 50)
			local coinsPage = data:GetCurrentPage()
			
			for rankInLB, dataStored in ipairs(coinsPage) do
				
				
				local name = game.Players:GetNameFromUserIdAsync(tonumber(dataStored.key))
				local coins = dataStored.value
				
				
				local template = script.Template:Clone()
				
				template.Name = name .. "Leaderboard"
				
				template.PlrName.Text = name
				
				template.Rank.Text = "#" .. rankInLB
				
				template.Gems.Text = coins
				
				template.Parent = script.Parent				
			end			
		end)
	end
end

I think using a loop will time out your data store. You should use a player added function and a player removing function, one to get data and the other to set data.

1 Like

Also its worth to mention local players in local servers, (when you use a local server to test your code), would error your script because local players have ids of -1 and Players:GetNameFromUserIdAsync

only takes things above 0, so you need an if statment to check if the players id is negative 1 then dont add them to the list.

but I a player have currency, for that leaderstat, also I have another leaderboard with the same exact script just a different currency name that works fine.

Uh do you understand what im saying? Im saying if you tested with a local server, your script would error assuming you havent added any checks to detect if the persons id is less then 0. Because sometimes in roblox we use Local servers to test our game and if you did that your leaderboard wouldn’t work. Since the local players data gets added to the leaderboard your leaderboard wouldn’t work at all after you tested with one.

Here is an example:

Look at the solution of this and youll understand

1 Like