PcoWSkbVqDnWTu_dm2ix
The Developer Hub is now deprecated and information on this page may no longer be accurate. Instead, you should go to our new and improved documentation. You can also learn more about the future of documentation.
Collapse Sidebar

ListKeysAsync

This function returns a DataStoreKeyPages object for enumerating through keys of a data store. It accepts an optional prefix parameter to only locate keys whose names start with the provided prefix.

If DataStoreOptions/AllScopes was set to true when accessing the data store through DataStoreService/GetDataStore, keys will be returned with all scopes as prefixes.

See Also

  • Articles/Data store|Data Stores, an in-depth guide on data structure, management, error handling, etc.

Parameters

Name Type Default Description

(Optional) Prefix to use for locating keys.

int
0

(Optional) Number of items to be returned in each page.

Returns

Return Type Summary

A DataStoreKeyPages instance that enumerates the keys as DataStoreKey instances.


Code Samples


List Keys by Prefix

local DataStoreService = game:GetService("DataStoreService")

local options = Instance.new("DataStoreOptions")
options.AllScopes = true

local buildingsStore = DataStoreService:GetDataStore("BuildingTypes", "", options)

-- Search keys by prefix "house"
local listSuccess, pages = pcall(function()
	return buildingsStore:ListKeysAsync("house")
end)
if listSuccess then
	while true do
		local items = pages:GetCurrentPage()
		for _, v in ipairs(items) do
			local value = buildingsStore:GetAsync(v.KeyName)
			print("Key: ", v.KeyName, "Value: ", value)
		end
		if pages.IsFinished then
			break
		end
		pages:AdvanceToNextPageAsync()
	end
end