Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I view all the keys in a DataStore?

I've been getting some visits at my place, and I was curious about who exactly was visiting. So, I made a datastore for storing visitors' basic info in keys named by their user ID's. Then I realized that I won't know how to actually look at all the keys I have in that store. Is there any way that I can list all the keys in the store?

datastore = game:GetService("DataStoreService"):GetDataStore("Visitors")
http = game:GetService("HttpService")

function onPlayerAdded(player)
	local data = {}
	
	data.Name = player.Name
	data.MembershipType = player.MembershipType
	data.AccountAge = player.AccountAge
	
	datastore:SetAsync(player.userId, http:JSONEncode(data))
end



game.Players.PlayerAdded:connect(onPlayerAdded)

3 comments

2

That's a good question

0

@adark, There is a way.

0

@funyun, There are only 2 keys in a global datastore, that would be "key" & "value". The key would be the scope or the username then the "value" would be a boolean or number and etc.. But, it takes a special datastore (Most people dont know about) to access the "key" & "value".

2 answers

✓ Accepted answer

Sadly, there is not. What you can do is store an index of the rest of the keys from a known key:

datastore = game:GetService("DataStoreService"):GetDataStore("Visitors")
http = game:GetService("HttpService")

function onPlayerAdded(player)
    local data = {}

    data.Name = player.Name
    data.MembershipType = player.MembershipType
    data.AccountAge = player.AccountAge

    datastore:SetAsync(player.userId, http:JSONEncode(data))
	datastore:UpdateAsync("KEY_INDEX", function(oldData)
		local data = oldData or {}
		data[player.userId] = true --so as to avoid duplicate entries
		return data
	end))
end

game.Players.PlayerAdded:connect(onPlayerAdded)

And to get all the keys, just do:

for id in ipairs(datastore:GetAsync("KEY_INDEX")) do
	print(id)
end

Special index,value datastore

#EXAMPLE : NOT ANSWER

local Datastore =  game:GetService("DataStoreService"):GetOrderedDataStore("Visitors")
local Sync = Datastore:GetSortedAsync(false,10) -- False = scope (leave it false & 10 is the number of how many datas you will load : MAX IS 100)
local Page = Sync:GetCurrentPage() -- Gets the table

Datastore:SetAsync("Bob", 1)
Datastore:SetAsync("Bill", 2)
Datastore:SetAsync("Sam", 3)
Datastore:SetAsync("n00b", 1337)

for i,v in next,Page do
	local data = tostring(v.key) -- username or whatev
	local number = tostring(v.value) -- Value or what you saved under the "data" or "key"
	local numberindex = tonumber(i)
	
	print(data, number, numberindex)
end

If this does error, let me know asap I will fix it.

2 comments

0

I'll be studying this for a while. Thankya

0

No problem, if you got any more questions just PM me on ROBLOX.

This discussion is preserved as it was. New answers, comments, and votes are closed.