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)
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
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.