Hi Creators,
We have updated the Datastore ListKeysAsync()
and ListDataStoresAsync()
Lua APIs to support an optional cursor parameter so that it’s easier to enumerate all the datastores and keys in an experience.
Previously, you had to go through all the keys or datastores in one path or start over from the beginning, making the iteration process with a large number of keys or datastores vulnerable to disruptions like server shutdown or crashes. Additionally, you couldn’t easily distribute the workload across multiple servers. The cursor now allows you to halt or resume iterations, which means you can enumerate all the keys and datastores across multiple sessions or even servers. Some of the use cases include performing data schema migrations and deleting datastores.
Here are the sample code on how to resume enumerating your keys with a cursor:
local DataStoreService = game:GetService("DataStoreService")
-- Create and Populate Datastores
for count = 1, 70 do
local experienceStore = DataStoreService:GetDataStore("Store" ..
tostring(count), count)
local success, errorMessage = pcall(function()
experienceStore:SetAsync("Value" .. tostring(count), count)
end)
if not success then
print(errorMessage)
end
end
-------
local listSuccess, pages = pcall(function()
return DataStoreService:ListDataStoresAsync()
end)
if listSuccess then
while true do
local items = pages:GetCurrentPage()
for _, ds in ipairs(items) do
print(ds.DataStoreName, "| Created:", ds.CreatedTime)
end
if pages.IsFinished then
break
end
print("cursor: ", pages.Cursor)
-- fetches listdatastores but passes in the current cursor to get the next
-- set of datastores
pages = DataStoreService:ListDataStoresAsync("", 3, pages.Cursor)
end
end
This code block iterates over all datastores using ListDataStoresAsync
. Initially, we pass in the default “”
for the cursor, but on subsequent calls, we use the cursor value that we can get from the current page.
As always, your feedback is invaluable for us to keep improving the service. Feel free to post any questions you may have. Our team will monitor the posts regularly and get back to you.
Happy building!
The Roblox Creator Services Team