How to Print All keys of a datastore

:person_raising_hand: Hello I would like to know how could I Print every key from a datastore that starts with “Player_”

Thank you! :heart:

Put this in #help-and-feedback:scripting-support

my bad I clicked the wrong channel

my idea may be bad but you could make an extra datastore that only has the keys and number like 1 2 3 4 5 6 7 etc.
with all the keys that you want to store in the list (may be really bad)

I just discovered Memory Stores and I may use them!

dang I’ve been coding for 2 years now and I’m still learning so many things XD

1 Like

You could use the ListKeysAsync method on your datastore, and then iterate through the Pages instance.

Here’s a code example:

local DataStore = game:GetService("DataStoreService"):GetDataStore("YourDataStoreName") --Get the datastore

local PREFIX = "Player_"
local AMOUNT_OF_KEYS = 50 --Amount of keys you'd like to get/print.

local keys --Placeholder variable for our keys pages
local success, err = pcall(function() --In case the retrieval request fails, we should handle the error.
	keys = DataStore:ListKeysAsync(PREFIX,AMOUNT_OF_KEYS) -- Retrieve the keys
end)
if(success and keys)then
	while not keys.IsFinished do --While there are still pages:
		task.wait()
		local keysInPage = keys:GetCurrentPage() --Get the array of keys from the current page

		for _, keyObject: DataStoreKey in pairs(keysInPage) do --Iterate through all the keys in the page
			print(keyObject.KeyName) --Print the key
		end

		keys:AdvanceToNextPageAsync() --Advance to the next page of keys
	end 
elseif(err) then --If there's an error, and not just empty keys
    warn(err)  --Print the error as a warning
end
5 Likes

Imma try this right now! Thank for you helping me