Ordered Datastore doesn't display every player's data

So I wanted to make a global leaderboard but ordered datastore doesn’t work for me properly. Some people reported to me that they should be on a leaderboard but they aren’t.

I used this script to print out 10 users with the most points

local DataStoreService = game:GetService("DataStoreService")
local PointsODS = DataStoreService:GetOrderedDataStore("playerData")

local function printTopTenPlayers()
	local isAscending = false
	local pageSize = 10
	local pages = PointsODS:GetSortedAsync(isAscending, pageSize)
	local topTen = pages:GetCurrentPage()

	-- The data in 'topTen' is stored with the index being the index on the page
	-- For each item, 'data.key' is the key in the OrderedDataStore and 'data.value' is the value
	for rank, data in ipairs(topTen) do
		local name = game.Players:GetNameFromUserIdAsync(data.key)
		local points = data.value
		print(name .. " is ranked #" .. rank .. " with " .. data.value .. "points")
	end

	-- Potentially load the next page...
	--pages:AdvanceToNextPageAsync()
end

-- Display the top ten players
printTopTenPlayers()

Desktop 15-08-2022 20-55-23-788
Results the script prints
Desktop 15-08-2022 20-47-29-175
How many points I have (I should be on the first place)

1 Like

Seems like a saving issue. Show how you save the stats

local function saveData(player)
	if sessionData[player.UserId] then
		local tries = 0	
		local success
		repeat
			tries = tries + 1
			success = pcall(function()
				playerData:SetAsync(player.UserId, sessionData[player.UserId])
			end)
			if not success then wait(1) end
		until tries == 3 or success
		
		if success then
			--print("Data saved for", player)
		else
			warn("Failed to save data for", player)
			return
		end
	else
		print("No session data for", player)
	end
end
local function autoSave()
	while wait(60) do
		for index, player in pairs(Players:GetPlayers()) do
			saveData(player)
		end
	end
end

Hey there :wave:

There are lots of tutorials out there on youtube on how to make an ordered datastore. You should check those out first. I personally recommend Gamerm8’s.

There is also a devforum on how to make one of these:

Happy to help :slightly_smiling_face:

I used it but it still shows the same players

hmm… do you refresh it every few seconds or so? Are there any stats to update at all?? Also, is the leaderboard in a surface gui / part or in a regular gui?

yeah i refrest it every minute and the leaderboard is a surface gui

Make sure you update the frames on the surface gui too.

Can i have the full script pls?

local sg = script.Parent --Surface GUI
local sample = script:WaitForChild("Sample") --Our Sample frame
local sf = sg:WaitForChild("ScrollingFrame") --The scrolling frame
local ui = sf:WaitForChild("UI") --The UI list layout

local dataStoreService = game:GetService("DataStoreService")

local dataStore = dataStoreService:GetOrderedDataStore("playerData")
wait(10)
while true do
	for i,plr in pairs(game.Players:GetChildren()) do--Loop through players
		if plr.UserId>0 then--Prevent errors
			local ps = game:GetService("PointsService")--PointsService
			local w = ps:GetGamePointBalance(plr.UserId)--Get point balance
			if w then
				pcall(function()
				--Wrap in a pcall so if Roblox is down, it won't error and break.
					dataStore:SetAsync(plr.UserId,function(oldVal)
				        --Set new value
						return tonumber(w)
					end)
				end)
			end
		end
	end    
	local smallestFirst = false
    local numberToShow = 10
    local minValue = 0 
    local maxValue = 10^30 
    local pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
    --Get data
    local top = pages:GetCurrentPage()--Get the first page
	local data = {}--Store new data
	for a,b in ipairs(top) do--Loop through data
		local userid = b.key--User id
		local points = b.value--Points
		local username = "[Failed To Load]"--If it fails, we let them know
		local s,e = pcall(function()
		 username = game.Players:GetNameFromUserIdAsync(userid)--Get username
		end)
		if not s then--Something went wrong
		   warn("Error getting name for "..userid..". Error: "..e)
		end
		local image = game.Players:GetUserThumbnailAsync(userid, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
		--Make a image of them
		table.insert(data,{username,points,image})--Put new data in new table
	end
	ui.Parent = script
	sf:ClearAllChildren()--Remove old frames
	ui.Parent = sf
	for number,d in pairs(data) do--Loop through our new data
		local name = d[1]
		local val = d[2]
		local image = d[3]
		local color = Color3.new(1,1,1)--Default color
		if number == 1 then
			color = Color3.new(1,1,0)--1st place color
		elseif number == 2 then
			color = Color3.new(0.9,0.9,0.9)--2nd place color
		elseif number == 3 then
			color = Color3.fromRGB(166, 112, 0)--3rd place color
		end
		local new = sample:Clone()--Make a clone of the sample frame
		new.Name = name--Set name for better recognition and debugging
        new.LayoutOrder = number--UIListLayout uses this to sort in the correct order
		new.Image.Image = image--Set the image
		new.Image.Place.Text = number--Set the place
		new.Image.Place.TextColor3 = color--Set the place color (Gold = 1st)
		new.PName.Text = name--Set the username
		new.Value.Text = val--Set the amount of points
		new.Value.TextColor3 = color--Set the place color (Gold = 1st)
		new.PName.TextColor3 = color--Set the place color (Gold = 1st)
		new.Parent = sf--Parent to scrolling frame
	end
	wait()
	sf.CanvasSize = UDim2.new(0,0,0,ui.AbsoluteContentSize.Y)
	wait(60)
end

Well, assuming you have everything set up correctly inside the surface gui, that should be working.

If all else fails, I strongly suggest Gamerm8’s tutorial on how to do this. It is simple and convenient.

This tutorial saves the top 100 highest cash values.

Let me know if it works :upside_down_face:

the leaderboard works fine it’s just that it doesn’t show the right people

And by that you mean that it isn’t updating or it is showing them in the wrong order?? I need more of a description…

The person in the 1st place has 2207 points but I have 910376 points and im not on the leaderboard

hmm…

How did you update your points?? Did you type it into the command console, or change it in the folder while playing??

I have found that certain scripts won’t save if you change it via commands, etc.

If this is the case, try adding in a part to give you points instead of commands.

datastores load the points fine it’s just that ordered datastores don’t work how they’re supposed to

Is there a way you can send me the place file or an uncopylocked link? I really am lost on what to do… Maybe I can check it out??

ok I figured out how to fix it. I just needed to make a new datastore and save the points from the old datastore to the new one