Looping through datastores

Are you able to loop through keys in a datastore??

basically my use case is.

i store tables with data in a datastore and want to see if the clients data matches any data stored by other players in the datastore

If you wish to loop through entries in a datastore, I’d say the best way to minimize requests and have it done would be to use an ordered datastore. Even then, if you are looking for specific data you should really just be using the keys. This is an extremely inefficient way of doing it.

do you suggest any better ways for example just say I’m storing a players win count and i want to see how many other players have the same win count how’d i go about that?

When a player leaves and joins, you could save their win data with the key being their amount of wins, and also save their win data with the key being their userid. So save it in two datastores. If you had a key being their amount of wins, you would be able to save a table of users there. Though, eventually, you might have some problems with datastore limits.

so to simplify are you suggesting that the key for the datastore is the win count and it’d have a field of the playerid to identify the player??

I was suggesting two datastores.
Alternatively, you could use a web server as roblox datastores are not great at doing what you are wanting.
For example, mysql can let you index multiple keys.

that’d force me to learn long polling

Fetch the user’s wins and cache them upon joining:

local DSS = game:GetService("DataStoreService")
local store = DSS:GetDataStore("wins")

game:GetService("Players").PlayerAdded:Connect(function(plyr)
    local s, wins = pcall(function(); store:GetAsync(plyr.UserId))
    wins = wins or 0
    plyr:SetAttribute("Wins", wins)
end)

Then you can iterate through the cache of all players using a method like this.

Does this help?

that’d only work for players in game smh

You didn’t specify.

@seancool07777’s methods work.

Here is how to use an ordered datastore.

nope that wont work it cant store tables

That has nothing to do with storing tables?

game.Players.PlayerRemoving:Connect(function(Player)
	local SaveData =  {}
	for _,Child in pairs(Player:WaitForChild("PlayerValues"):GetChildren())do
		table.insert(SaveData,Child.Value)
	end
	DataStore:SetAsync(Player.UserId, SaveData)
end)

thats my current storing method wont work with an ordered datastore

slight issue with the script on the roblox docs

https://gyazo.com/776f8a2ec15b1edfcab2823c5ceebc80

for _, entry in pairs(DataStore) do

what is it actually meant to index??