OrderedDataStore's topTen resulting in "{}"?

So I was trying to make an ordered list based on my data store to make a leaderboard. Currently, the DataStore is saving the seconds played. So I was following the DevHub post about OrderedDataStore, and here’s what my code looks like:

local DataStoreService = game:GetService("DataStoreService")
repeat wait() until DataStoreService:GetOrderedDataStore("TimePlayed")~= nil
local timeODS = DataStoreService:GetOrderedDataStore("TimePlayed")
while wait(1) do
	local isAscending = false
	local pageSize = 10
	local pages = timeODS:GetSortedAsync(isAscending,pageSize)
	print(pages)
	topTen = pages:GetCurrentPage()
	print(topTen)
	
	for rank, data in ipairs (topTen) do
		local name = data.key
		local points = data.value
		print(data.key .. "is ranked #" .. rank .. "with " .. data.value .. "Seconds")
	end
end

The print(pages) is printing the word “Instance”. How would I fix this, and why is it printing that?

The thing is that :GetSortedAsync() doesn’t return a table or anything like that which you can print, really. Instead, it returns a “Data Store Page,” which is indeed an instance. To get some sort of printable material, you need to run a method like :GetCurrentPage() (as you do in the next line).

@Chatowillwin I forgot to mention, the next print prints {} even though I have a value in the dataStore

Oh, that’s interesting. The only reason I’d assume it prints an empty table is because there is, indeed, no data saved to that datastore. As usual when it comes to datastores, make sure that “Enable studio access to API services” is enabled, and that you double-check that you definitely saved it to the proper OrderedDataStore.

@Chatowillwin I wish I could take a screenshot I’m using a computer that isn’t mine and it’s linux so it doesn’t have the Windows 10 Snipping Tool, but you can save some data in there yourself and see I guess. (API Services Access is enabled)
MonstercatBeta.rbxl (78.3 KB)
It’s the OrderedDataStore script in ServerScriptService

I use Datastore Editor to see my data

Can’t open studio right now. Something I will say is that you are running a :GetSortedAsync() every 1 second with this script, which is way too often since there’s a pretty heft cooldown needed for that function (I forget how long). Either way, that isn’t the answer to the issue you’re having, so I really can’t think of what else the problem would be other than that you simply didn’t save the data properly-- have you tried using the command bar to see what happens when you try printing a :GetAsync() for the key of the data you saved?

@Chatowillwin Hmmm… Typing game:GetService("DataStoreService"):GetDataStore("TimePlayed"):GetAsync(1463962029) on the server side is printing the value. I don’t know what to do. I was mainly having the while wait(1) there for testing, and changing it to 30 or 60 when done. But wouldn’t it still print things for the first couple of times before reaching the limit?

Any other suggestions?

You’re correct that the limit shouldn’t prevent it from properly printing the value the first few times. I must be missing something obvious here, because I am stumped as to how your script wouldn’t work with what I see. :thinking:

Yeah I’m looking at everything multiple times to try to find something :thinking:

@Chatowillwin I wonder if it’s because it only works in live game. I don’t know why that would be, but have you ever done OrderedDataStore and had it work in Solo Test?

I can’t test live game lol, can you?

It results in printing {}, as there isn’t any data saved to the OrderedDataStore, I don’t see where you save that data to OrderedDataStores in the place file you gave either.

What you should do is everytime the loop runs, do a loop through all players in the game then you can use UpdateAsync on the OrderedDataStore and the Key being the player UserId, and set it to the value you want it to be.

for _, player in pairs(game.Players:GetPlayers()) do
     local timePlayed = player.TimePlayed.Value --Reference to Some Value That you want to save
     pcall(timeODS.UpdateAsync, timeODS, player.UserId, function()
          return timePlayed
     end)
end

@WaterJamesPlough I have a saving script that has been working. I know this because DataStore Editor is showing me that the data is there, and it also prints the seconds. Here’s the TimePlayedManager in ServerScriptService that is saving correctly:

game.Players.PlayerAdded:Connect(function(player)
	local DataStoreService = game:GetService("DataStoreService")
	local dataStore = DataStoreService:GetDataStore("TimePlayed")
	
	local ReplicatedStorage = game:GetService("ReplicatedStorage")
	local timeRemoteEvent = ReplicatedStorage:WaitForChild("TimeRemoteEvent")
	
	local success, err = pcall(function()
		local countSecondsCoro = coroutine.create(function()
			while wait(5) do
				dataStore:IncrementAsync(player.UserId,5)
				local timePlayed = dataStore:GetAsync(player.UserId)
				print(timePlayed)
				timeRemoteEvent:FireClient(player,timePlayed)
			end
		end)
		coroutine.resume(countSecondsCoro)
	end)
	
end)

EDIT: I’d assume not, but would I have to have the script that saves the data say GetOrderedDataStore instead of GetDataStore?

EDIT OF EDIT: Yes that’s working…

Well I guess I fixed it myself