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?
01 | datastore = game:GetService( "DataStoreService" ):GetDataStore( "Visitors" ) |
02 | http = game:GetService( "HttpService" ) |
03 |
04 | function onPlayerAdded(player) |
05 | local data = { } |
06 |
07 | data.Name = player.Name |
08 | data.MembershipType = player.MembershipType |
09 | data.AccountAge = player.AccountAge |
10 |
11 | datastore:SetAsync(player.userId, http:JSONEncode(data)) |
12 | end |
13 |
14 |
15 |
16 | 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:
01 | datastore = game:GetService( "DataStoreService" ):GetDataStore( "Visitors" ) |
02 | http = game:GetService( "HttpService" ) |
03 |
04 | function onPlayerAdded(player) |
05 | local data = { } |
06 |
07 | data.Name = player.Name |
08 | data.MembershipType = player.MembershipType |
09 | data.AccountAge = player.AccountAge |
10 |
11 | datastore:SetAsync(player.userId, http:JSONEncode(data)) |
12 | datastore:UpdateAsync( "KEY_INDEX" , function (oldData) |
13 | local data = oldData or { } |
14 | data [ player.userId ] = true --so as to avoid duplicate entries |
15 | return data |
16 | end )) |
17 | end |
18 |
19 | game.Players.PlayerAdded:connect(onPlayerAdded) |
And to get all the keys, just do:
1 | for id in ipairs (datastore:GetAsync( "KEY_INDEX" )) do |
2 | print (id) |
3 | end |
Special index,value datastore
01 | local Datastore = game:GetService( "DataStoreService" ):GetOrderedDataStore( "Visitors" ) |
02 | 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) |
03 | local Page = Sync:GetCurrentPage() -- Gets the table |
04 |
05 | Datastore:SetAsync( "Bob" , 1 ) |
06 | Datastore:SetAsync( "Bill" , 2 ) |
07 | Datastore:SetAsync( "Sam" , 3 ) |
08 | Datastore:SetAsync( "n00b" , 1337 ) |
09 |
10 | for i,v in next ,Page do |
11 | local data = tostring (v.key) -- username or whatev |
12 | local number = tostring (v.value) -- Value or what you saved under the "data" or "key" |
13 | local numberindex = tonumber (i) |
14 |
15 | print (data, number, numberindex) |
16 | end |
If this does error, let me know asap I will fix it.