Getting all Data from a Datastore

Im currently working on an AdminSystem.
I want the game to get all saved Admins and Supporters, which have been added to the game. But for some reason, its not working. The loop is working but the print command is giving nil (And yes, I saved already Data in the Datastore). I found this code example but I cant just make it work. Its a script inside ServerScriptService. As DataStoreKey, I am using the players Name.

local DataStoreService = game:GetService("DataStoreService")
local AdminStore = DataStoreService:GetDataStore("AdminList1")


local succes, pages = pcall(function()
	return AdminStore:ListKeysAsync()
end)

print(succes, pages)
if succes then
	while true do
		local items = pages:GetCurrentPage()
		for i, v in pairs(items) do			
			local value = AdminStore:GetAsync(v)
			print(value)			
		end
			
		if pages.IsFinished then			
			break
		end
		
		pages:AdvanceToNextPageAsync()
		
	end
end

So, every player has a copy of AdminStore as private data?

Is there not one datastore for everyone to use?

Might be wrong, but Iā€™m not really understanding whatā€™s supposed to happen.

1 Like

It is one datastore that Im using. I want to get all the values which are saved in the datastore and not from one single player.

2 Likes
local DataStoreService = game:GetService("DataStoreService")
local AdminStore = DataStoreService:GetDataStore("AdminList1")

local success, pages = pcall(function()
	return AdminStore:ListKeysAsync()
end)

print(succes, pages)
while success and not pages.IsFinished do
	local items = pages:GetCurrentPage()
	for _, v in items do
		local s, value = pcall(AdminStore.GetAsync, v)
		print(value)			
	end
	pages:AdvanceToNextPageAsync()
end

Donā€™t know if this did anything but you could give it a go.

print(succes, pages)
while succes and not pages.IsFinished do
	local items = pages:GetCurrentPage()
	for _, v in items do
		local s, value = pcall(function()
			return AdminStore:GetAsync(v)
		end)
		print(value)			
	end
	pages:AdvanceToNextPageAsync()
end

I tried it now with that loop based on yours, but your pcall function wouldnt work like that, but I still get nil for the value and ā€œtrue, instanceā€ for the succes,pages print.

What type is ā€˜valueā€™ intended to be, not what it prints?

1 Like

This might work:

local DataStoreService = game:GetService("DataStoreService")

local AdminStore = DataStoreService:GetDataStore("AdminList1")

local success, pages = pcall(AdminStore.ListKeysAsync, AdminStore)

if success then
	while true do
		local items = pages:GetCurrentPage()

		for rank, data in items do
			print(data.Value)
		end

		if pages.IsFinished then break end
		pages:AdvanceToNextPageAsync()
	end
end

The items table has the rank as the key and the DataStore data as the value

Edit: Alternatively data.value might work if data.Value results in an error

Edit 2: @Faustollol Please ignore what I wrote above this, because I found the solution:

local DataStoreService = game:GetService("DataStoreService")

local AdminStore = DataStoreService:GetDataStore("AdminList1")

local success, pages = pcall(AdminStore.ListKeysAsync, AdminStore)

if success then
	while true do
		local items = pages:GetCurrentPage()

		for index, dataStoreKey in items do
			local success, value = pcall(AdminStore.GetAsync, AdminStore, dataStoreKey.KeyName)

			if success then print(value) end
		end

		if pages.IsFinished then break end
		pages:AdvanceToNextPageAsync()
	end
end

The items table actually has a DataStoreKey Instance as a value that has the property KeyName which represents the DataStoreā€™s key. The problem you were getting was because you were using DataStoreKey directly instead of the KeyName property

1 Like

Yes this makes so much sense now! Thank you.

2 Likes

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