How can I see how much data is in a datastore?

How can I see how much data is in a datastore?

So basically every time you use lets say SetAsync with a datastore, data is now stored inside the datastore, how can I get how much data is in said datastore?

If you mean data size by “how much data in in said datastore” you can use JSONEncode for that.

Below is a possible example on how to use JSOnEncode to find how large data is in numbers.

local DSS = game:GetService("DataStoreService")
local HTTP = game:GetService("HttpService")

local PlayerData = DSS:GetDataStore("PlayerData")

local suc,dat = pcall(function()
    return PlayerData:GetAsync("Inventory")
end)

-- gets the length of the string and turns it into a number
print(string.len(HTTP:JSONEncode(dat)))

You can use listkeysasync to iterate over elements in the datastore: DataStore | Documentation - Roblox Creator Hub

If you have many keys inside, it might take a (very) long time to iterate over them due to the rate limits.

If you want to edit the same data for every user, the recommended way of doing this is to edit their data when they join. This way the load is spread across multiple servers/days.

Ok this gets stuff thats in an async, I would like to get all data in a datastorage, thanks though

So this requires me too input a number of how many pages to go through at max, ofc I dont know this number which is why I need to “see how much data is in a datastore”

Thanks though

The number you give is the maximum number of keys it can return. I’m pretty sure the max is 100 (you can try higher values and see if that works…).

Here’s code that should count the keys in your datastore. If your datastore has many keys, you’ll run into rate limits (use waits, or just retry using pcalls).

local dataStore = game:GetService("DataStoreService"):GetDataStore("MyDatastore")
local pages = dataStore:ListKeysAsync(nil, 100)
local counter = 0
while true do
	for _, data in pairs(pages:GetCurrentPage()) do
		print(data.KeyName)
		counter += 1
	end
	if pages.IsFinished then
		break	-- Exit loop if we've looped over all pages
	end
	-- Get new page (same API limit as calling 'ListKeysAsync')
	pages:AdvanceToNextPageAsync()
end
print(counter)

This is the only way to loop over a datastore without having knowledge of the keys.

Ok but if I can only get up too 100 how can I get pages after 100?

The loop keeps going to next pages. There’s no way to get all keys in a single API call if you have too many keys.

Please try out the code.

2 Likes

I didnt really read the code you sent and I see now itll probably work so thanks lol
(Mb for being a little harsh if I was)
Let me go test it out!

1 Like

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