Global leaderboard only goes from 1-5

--// SERVICES \\--
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--// OBJECTS TO CLONE \\--
local objectsToClone = ReplicatedStorage:WaitForChild("ObjectsToClone")
local template = objectsToClone:WaitForChild("Template")
local levelsODS = DataStore:GetOrderedDataStore("Level")

--// VARIABLES \\--
local coinPusher = workspace.CoinPusher
local screens = coinPusher.Screens
local leaderboardScreen = screens.LeaderboardScreen.Display.SurfaceGui.List
local lobby = coinPusher.Lobby
local statuePositions = lobby.StatuePositions
local firstPlace = statuePositions.FirstPlace
local secondPlace = statuePositions.SecondPlace
local thirdPlace = statuePositions.ThirdPlace
local statueHolder = lobby.StatueHolders

--// CONFIGS \\--
local refreshRate = 120

--// FUNCTIONS \\--
local function CreatePlayerStatue(playerUserId, placePosition)
	local playerStatue = Players:CreateHumanoidModelFromUserId(playerUserId)
	playerStatue.Name = Players:GetNameFromUserIdAsync(playerUserId)
		
	playerStatue:SetPrimaryPartCFrame(placePosition.CFrame * CFrame.new(0,playerStatue.Humanoid.HipHeight * 2.25,0))
	
	playerStatue.Parent = statueHolder
	
	playerStatue.PrimaryPart.Anchored = true
end

local function updateLeaderboards()

	local success, errorMessage = pcall(function()

		local Data = levelsODS:GetSortedAsync(false, 20)
		local LevelsPage = Data:GetCurrentPage()

		for rank, data in ipairs(LevelsPage) do
			
			if tonumber(data.key) > 0 then
				local userName = Players:GetNameFromUserIdAsync(tonumber(data.key))
				local userId = Players:GetUserIdFromNameAsync(userName)
				local level = data.value
				local isOnLeaderboard = false
				
				if level > 0 and isOnLeaderboard == false and userId > 0 then
					local NewLBFrame = template:Clone()
					local PlayerName = NewLBFrame:FindFirstChild("PlayerName")
					local CurrentLevel = NewLBFrame:FindFirstChild("Levels")
					local Rank = NewLBFrame:FindFirstChild("Rank")
					
					PlayerName.Text = userName
					CurrentLevel.Text = level
					Rank.Text = "#"..rank
					
					print("Created "..tostring(userName).." "..tostring(rank))
					
					NewLBFrame.Parent = leaderboardScreen
					

					if rank == 1 then
						PlayerName.TextColor3 = Color3.new(1, 0.705882, 0.227451)
						CurrentLevel.TextColor3 = Color3.new(1, 0.705882, 0.227451)
						Rank.TextColor3 = Color3.new(1, 0.705882, 0.227451)
						CreatePlayerStatue(userId, firstPlace)
					elseif rank == 2 then
						PlayerName.TextColor3 = Color3.new(0.721294, 0.721279, 0.721279)
						CurrentLevel.TextColor3 = Color3.new(0.721294, 0.721279, 0.721279)
						Rank.TextColor3 = Color3.new(0.721294, 0.721279, 0.721279)
						CreatePlayerStatue(userId, secondPlace)
					elseif rank == 3 then
						PlayerName.TextColor3 = Color3.new(0.736507, 0.487678, 0.259174)
						CurrentLevel.TextColor3 = Color3.new(0.736507, 0.487678, 0.259174)
						Rank.TextColor3 = Color3.new(0.736507, 0.487678, 0.259174)
						CreatePlayerStatue(userId, thirdPlace)
					elseif rank == 6 then
						print(userName)
					end 
				end
			end
		end
	end)

	if not success then
		warn(errorMessage)
	end

end

while true do
	
	for _, player in pairs(Players:GetPlayers()) do
		
		local stat = player.leaderstats.Level

		levelsODS:SetAsync(player.UserId, stat.Value)
	end
	
	for _, frame in pairs(leaderboardScreen:GetChildren()) do
		if frame:IsA("Frame") and frame.Name ~= "Title" then
			frame:Destroy()
			frame = nil
		end
	end
	
	for _, statue in pairs(statueHolder:GetChildren()) do
		statue:Destroy()
		statue = nil
	end
	
	updateLeaderboards()
	
	print("Updating leaderboard")
	
	task.wait(refreshRate)
end

I followed a tutorial on global leaderboards as DataStores are something I don’t do well with. The global leaderboard works fine until the 5th player. It displays the players from Rank 1 to Rank 5 but then it jumps to Rank 9. What could be causing the issue and how could I fix this?

Thank you!

got a screenshot of what it is doing on the leader?

also what is your printout from this

1 Like

I’m not sure how to send a screenshot but output is:

Created TiledTexan 1
Created efarswognunya 2
Created UnderEvaluation 3
Created RealDad4real 4
Created karenalyse 5
Created shqdows639 9

I don’t understand why its skipping over 6-8, as it should be getting it in order from 1-10

that seems to be working fine from the output

I’m confused is it supposed to go from 1-5 and then skip to 9?

yeah it can there aren’t anymore players between 5-9 with ranks or levels that have been saved yet

as more players join and get different level/ranks it will fill in the numbers between

seems right now you have only had 6 players play and it actually save their level/rank to the leaderdatastore

if you only want the top 5 in the leaderboard then change the 20 here to 5

But if there are only 6 players wouldn’t it go from 1-6? I’m still not sure why it would go to 9.

that players data was set to or saved as rank or level 9

its because you are requesting the first page of the orderdatastore and getting the first 20 data slots just change the 20 to 5 and it will limit it to just the first 5

I’m starting to understand what you mean. However, how could I display the 6 players in order from Rank 1 to Rank 6? I want it to show the top 10 people in the game. So it would look like:

Created TiledTexan 1
Created efarswognunya 2
Created UnderEvaluation 3
Created RealDad4real 4
Created karenalyse 5
Created shqdows639 6

just change that 20 to 10 and once the data is filled in it will show the first 10

actually think it needs to be

local Data = levelsODS:GetSortedAsync(false, 10)  -- actually this is right here for them to be in order had to double check myself been long day

I changed it from 20 to 10 but it’s still displaying 1-5 and then 9

yeah its going to until the data is filled in or if you changed shqdows639 rank or level in the data to 6

it should be like this to be in order