Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How would I view all the keys in a DataStore?

Asked by
funyun 958 Moderation Voter
9 years ago

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)
2
That's a good question Shawnyg 4330 — 9y
0
@adark, There is a way. MessorAdmin 598 — 9y
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". MessorAdmin 598 — 9y

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

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
Ad
Log in to vote
1
Answered by 9 years ago

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.

0
I'll be studying this for a while. Thankya funyun 958 — 9y
0
No problem, if you got any more questions just PM me on ROBLOX. MessorAdmin 598 — 9y

Answer this question