You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
List keys’ name from a datastore.
What is the issue? Include screenshots / videos if possible!
Not all keys are returned.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Looked through Dev Hub and Roblox Documentation.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
Okay so, I want to get all the keys from a datastore. I stored 2 keys in a datastore as an experiment, and tried to print these 2 keys’ name. I do so by getting pages of the datastore by using ListKeysAsync(), setting the page size to be 10, so that the 2 keys can be listed in a single same page. Then, I get the first page of the pages, loop through the keys inside the page and print it’s index and key name. However, the output only shows the key name of the first key, but it doesn’t return the second key. This means that there was only 1 key in the page, so where did the second key go? I’m not sure what went wrong. Thank you for helping, I appreciate it.
local dataStoreService = game:GetService("DataStoreService")
local playerDataStore = dataStoreService:GetDataStore("PlayerData")
print("== Data Added ==")
playerDataStore:SetAsync("Test1", 1)
playerDataStore:SetAsync("Test2", 2)
print(playerDataStore:GetAsync("Test1"))
print(playerDataStore:GetAsync("Test2"))
print("== Key List ==")
local pages = playerDataStore:ListKeysAsync("",10)
local page = pages:GetCurrentPage()
for i, key in pairs(page) do
print(i, key.KeyName)
end
playerDataStore:RemoveAsync("Test1")
playerDataStore:RemoveAsync("Test2")
print("== Data Removed ==")
print(playerDataStore:GetAsync("Test1"))
print(playerDataStore:GetAsync("Test1"))
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.
If the page is not list via dictionary, use ipairs instead of pairs. If that doesn’t work, try using AdvanceToNextPageAsync() in a while loop until it is finished.
Hi, thanks for the helping! Yup, the page is not list via dictionary, so I change it to ipairs instead. However, the output remains the same, missing the second key. Then, I check whether there is next page by using AdvanceToNextPageAsync(), which return nil, meaning there is no second page, first page is all it got. Hence there is no need to loop through all the pages until it is done.
-- How I check there is no next page after current page
local page = pages:GetCurrentPage() -- return [1] = Instance
page = pages:AdvanceToNextPageAsync() -- return nil
So the question is, where did the second key go? Why isn’t it showing in the list created using ListKeyAsync() (Lists all keys in a data store). Unless I am using this method wrongly. Then what is the proper way of using ListKeyAsync()?
Yup, I understand where you are coming. There are many ways to store keys and obtain them. Like creating a table for keys and then store it in another key value in a data store. But I want to know why ListKeyAsync(), which its purpose is to lists all keys in a data store, isn’t working as intentioned. Unless I’m using it the wrong way, or have the wrong concept of that method.
Actually, you are right! I figured the problem. It was due to my lack of understanding of pages and have been coding it wrongly. There is nothing wrong with ListKeyAsync(). All I need to do is properly write a code that loop the pages until it is finished!
So here is the solution:
local dataStoreService = game:GetService("DataStoreService")
local playerDataStore = dataStoreService:GetDataStore("PlayerData")
local pages = playerDataStore:ListKeysAsync()
repeat
local page = pages:GetCurrentPage()
for i, v in page do
print(page[i].KeyName)
end
pages:AdvanceToNextPageAsync()
until pages.IsFinished