How To Get All the data saved inside a datastore

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to get all the data saved inside a datastore

  1. What is the issue? Include screenshots / videos if possible!

I’m storing Data as a table : inventory= {stats = value, rank = value…}
And i don’t know how to get the data of all the players from it

7 Likes

https://developer.roblox.com/en-us/api-reference/function/GlobalDataStore/GetAsync

not really helpful, i tried to read it before but it’s just the usual one, i want to get all the user ids, and their values into a single table

https://developer.roblox.com/en-us/api-reference/function/DataStore/ListKeysAsync
ListKeysAsync for a list of a datastore’s keys and then GetAsync to retrieve each key’s assigned value.

1 Like

i tired all is written but i didn’t understand, can you do it for me? i want it to load all the keys in the datastore
for examples banned ones, it’ll get all of their ids

1 Like

If I understand what you are trying to do here- you are wanting to pull all player’s (who have data stored) user ID & saved inventory. Correct me if I’m wrong on that.

To do this you could have a function as so- format and replace variable names how you deem fit!

-- Call DataStoreService and set an AllScopes parameter for later use
local dataService = game:GetService("DataStoreService")
local options = Instance.new("DataStoreOptions")
options.AllScopes = true
local DataStore = dataService:GetDataStore("<Insert Data Store Name Here>", "", options)

-- This will be used to store the retrieved userIDs and Inventory
-- Each key will be a players userID, and the content of the key will be their inventory!
local dataDictionary = {}

-- Get all the stored keys under our Data Store
local listSuccess, pages = pcall(function()
	return DataStore:ListKeysAsync()
end)

-- If the pcall succeeds without error, then this will run.
-- The loop will go on until all key values have been read through.
if listSuccess then
	while true do
		local items = pages:GetCurrentPage()
		for _, v in ipairs(items) do
            -- Take current key + its associated value and write it into the dictionary.
			local value = DataStore:GetAsync(v.KeyName)
			dataDictionary[v.KeyName] = value
		end
		if pages.IsFinished then
			break
		end
		pages:AdvanceToNextPageAsync()
	end
end

This was mostly taken from Roblox’s ListKeysAsync (as first sent by @Forummer).

For the code listed above to work in the way you want it to, the key used to store a player’s inventory under must be their UserID. As you wanted, it will neatly put everything into a dictionary table (dataDictionary) for you to access and use how you want!

This was my first ever forum post, so please correct me if there is anything clearly wrong here. Other than that though, if you are still confused with any of the comments I put in the code or have any questions- I’ll try to help you out the best I can!

Hope this helps! :smile:

26 Likes

Thank you so much, it worked fine

You’re welcome! Glad it helped!

1 Like

How do you get this to NOT overload the ROBLOX data stores?