Problem with ListKeysAsync()

I want to view all the keys in my data store but whatever I do I only get the number 3 as output on index 1
image
I looked for solutions in Developerhub but I found only one and it was magically fixed and I dont know how or why…

This is my code for an example datastore named “Test”:

local DSS = game:GetService("DataStoreService")
local testds = DSS:GetDataStore("Test")

--key: 1
--key: 3
--key: 135135151
--key: 145126233
--the keys have random values

local keys = testds:ListKeysAsync()
local pages = keys:GetCurrentPage()

for i,v in pairs(pages) do
	print(i,v.KeyName)
	if keys.IsFinished then
		break
	end
	keys:AdvanceToNextPageAsync()
end

Isn’t it supposed to return the keys names just like in the comments I added or am I not understanding something? Could it be a bug on roblox’s side? Any help is appreciated…

1 Like

If i’m not wrong, it’s because it’s an asynchronous function, which means you should code it as such:

local success, keys = pcall(function ()
     return testds:ListKeysAsync()
end)
1 Like

I tried it, didn’t fix it. Thank you for helping anyways.

1 Like

You’re advancing the page after every single key in the current page, and you should only do that once you’re done with the page. Your variable names seemed to be swapped. It’s difficult to provide a way to correct your code because this script uses keys to represent the page-providing instance of datastore keys and pages to represent the keys in the current page.

Once you rename them, try looping through the keys in the current page in a while loop that maintains itself as long as the pages aren’t finished.

2 Likes

How would I know when I am done with the page? Also if I swap the names only on the variables then the script breaks but of course nothing changes if I swap them on the whole script. I changed it to:

local DSS = game:GetService("DataStoreService")
local testds = DSS:GetDataStore("Test")

--key: 1
--key: 3
--key: 135135151
--kay: 145126233

local pages = testds:ListKeysAsync()
	
local keys = pages:GetCurrentPage()

while true do
	for i,v in pairs(keys) do
		print(i,v.KeyName)
	end
	if pages.IsFinished then
		break
	end
	pages:AdvanceToNextPageAsync()
end

and now it seems to print “1 3” 4 times and im assuming its because there are only 4 keys. In case there was a misunderstanding I want to print all the keys in the data store. I am very new to DataStores so I hope you understand my confusion.

1 Like

I also see 1 as the “i” multiples times in the for loop of datastore keys. That seems like a goof on Roblox’s part. Regardless, in my case the “v” of the array (the actual instances holding a key name in the datastore) are different. The only difference in my code is that I set keys to a new :GetCurrentPage() call after advancing but that shouldn’t affect looping through the first page. There should not be duplicate keys at all. Have you running the game in Roblox (after publishing) and checking the F9 console in-game to see if it’s different?

If you have deleted the “3” key from your datastore in the past, it might be returning the original deleted 3s as well. Have you tried setting a new key at the beginning of the script to see if it appears in the loop?

2 Likes

Isn’t “i” supposed to increment by 1 in every loop? Also by your code you mean what I replied to you or did you add code somewhere? I published and checked it in-game and it seems to print 5 times now I am not sure why… and the datastore is brand new, I just made it because I was having problems with this in another game and tried to test it on a different experience with a different datastore. edit: I tried setting a new key at the beggining but it only seems to increase the amount of printing to 6 times and didnt change anything else.

image

local DSS = game:GetService("DataStoreService")
local testds = DSS:GetDataStore("Test")

--key: 1
--key: 3
--key: 135135151
--kay: 145126233
testds:SetAsync("7",8)
local pages = testds:ListKeysAsync()

local keys = pages:GetCurrentPage()

while true do
	for i,v in pairs(keys) do
		print(i,v.KeyName)
	end
	if pages.IsFinished then
		break
	end
	pages:AdvanceToNextPageAsync()
	task.wait(1)
end
1 Like

I recreated my own code for listing keys, sorry I wasn’t clear about that. In the code you just sent, move the keys line into the first line of the while loop and add extra arguments to the ListKeysAsync call. I copied the script you just sent and tried to add those changes. I also removed i because Roblox uses some weird chunky way to loop through keys so that i will repeat at 1 multiple times.

local DSS = game:GetService("DataStoreService")
local testds = DSS:GetDataStore("Test")

--key: 1
--key: 3
--key: 135135151
--kay: 145126233
testds:SetAsync("7",8)
local pages = testds:ListKeysAsync(nil, 50, nil, true)

while true do
	local keys = pages:GetCurrentPage()
	for i,v in pairs(keys) do
		print(v.KeyName)
	end
	if pages.IsFinished then
		break
	end
	pages:AdvanceToNextPageAsync()
	task.wait(1) -- maybe not needed unless there's rate limiting in play
end
4 Likes

Thank you! It actually fixed it. So the only problem basically was the keys being outside the loop? Also whats the difference of :ListKeysAsync(nil, 50, nil, true) and :ListKeysAsync()?

1 Like

Yeah, the keys being outside meant they weren’t updated after advancing the pages. The only difference between those two calls is that the first uses true as the fourth argument, which makes it ignore any deleted keys. I guess in the past you had set the “3” key and deleted it so it kept coming up. The first three arguments are just the defaults that you’d get in the bare call with no arguments.

2 Likes

I see. I think the problem was that I was only printing the first key! Since the first key I set was 3 not 1 like in the comments of my code. Also by removing the parameters inside the () nothing really changes. I know you entered the default settings so it makes sense apart from the deleted keys part which doesn’t change anything since I didn’t delete any keys ever. Thank you for solving my problem roblox’s documentation isn’t really helpful…

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.