How would I go about loading all data from a DataStore?

Issue Summary

In my game, every player that joins gets a leaderstat. That leaderstat is a JSONEncoded string of an array. Every array is custom to that player, so it could change at any time, but in the end, you get a JSONEncoded string value. (I have all of that set up, + data saving).

What I am trying to do is load all of the string values when called upon, despite whether or not the player is in-game or not. I need to be able to print out all of the strings inside of that datastore that the players received when they played the game.

Tried Solutions

I did some research and found that OrderedDataStores was the way to do this. Unfortunately, I found that you can only store numerical values inside an ordered datastore, so that got me back to square one.

Is there anyway to loop through a DataStore, if not, how would I go about acvieving my goal?

2 Likes

You can do regular DataStore and at the same time you could pull the saved data at any given time as long as you have the key.

1 Like

How can I retrieve the key if the player is not in the game?

Let me see how you are saving the data.

For example if I’m saving the data in the following way:

playerData:SetAsync("PlayerData" .. Player.userId, DataToSave)

Then I can retrieve it from any server at any time like the following:

local data = playerData:GetAsync("PlayerData" .. id) -- id being the user id of the player you are retrieving the data from

Yeah I understand that part and have up to that, however my issue is that I don’t want to load the data of one specific player, but rather every player that has played the game.

For instance, my goal is to somehow get a list of all keys within a specific datastore, and then do stuff with that data, for example a Global Leaderboard.

But it seems this isn’t as easy as I think it is and I will have to find some sort of workaround to be able to print out data of all keys in a datastore.

Ahhhh. You should not load the data of every player.
Fact: You can not, even though you may if the game only has a few players. But as the number of players increases, the chances of getting all the data decreases. Therefore it is inefficient. So if you want to do a global leaderboard, heres an example:

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetOrderedDataStore("MostWins")


function ShowFrontPage(data)
	for i,v in pairs(data) do
		userid = v[1]
		value = v[2]
		-- You can update the UI leaderboard with this information.
	end
end

while true do
	for i,plr in pairs(game:GetService("Players"):GetPlayers()) do
		pcall(function()
			dataStore:UpdateAsync(plr.UserId,function(oldVal)
				return plr.leaderstats.Wins.Value -- Change with the new value
			end)
		end)
	end

    local smallestFirst = false--false = 2 before 1, true = 1 before 2
    local numberToShow = 25--Any number between 1-100, how many will be shown
    local minValue = 1--Any numbers lower than this will be excluded
    local maxValue = 10e30--(10^30), any numbers higher than this will be excluded
    local pages = dataStore:GetSortedAsync(smallestFirst, numberToShow, minValue, maxValue)
	
    local top = pages:GetCurrentPage()
	
    local data = {}--Store new data

    for a,b in ipairs(top) do--Loop through data
		local userid = b.key--User id
		local wins = b.value
        table.insert(data, {userid, wins})
    end



    wait(120)
end
1 Like

Thank you, I think I can figure out a way to make this work with string values and another leaderstat in my game.

I appreciate you taking the time to help me, thanks again!

1 Like