Get all keys from a datastore

How could I get all users [keys] from a datastore? I’m trying to make a script that gets all keys, clones a frame, and puts the data from the keys into that frame.

I’ve looked at past posts and followed what they said, but they didn’t seem to work.

Here is my code:

--//Datastores
local RouteDatastores = DataStoreService:GetDataStore("Routes")

--//Webhook
local webhookURL = "" 

function AddCurrentRoutes(player)
	-- Assuming ReplicatedStorage has a structure like ReplicatedStorage.Assets.Template
	local Template = ReplicatedStorage.Assets.Template
	local PlayerGui = player:WaitForChild("PlayerGui")
	local RoutesGui = PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Routes")
	local List = RoutesGui.RoutesList

	-- Use ListKeys instead of ListKeysAsync to fetch keys
	local success, keys = pcall(function()
		local pages = RouteDatastores:ListKeysAsync("", 30)
		return pages:GetCurrentPage()
	end)

	if not success then
		warn("Failed to fetch data from the datastore")
		return
	end

	for _, entry in pairs(keys) do
		local Clone = Template:Clone()
		local RouteData = RouteDatastores:GetAsync(entry)

		if RouteData then
			Clone.Parent = List
			Clone.Host.Value = tostring(RouteData.Host)
			Clone.Time.Value = tostring(RouteData.Time)
			Clone.Code.Value = tonumber(RouteData.Code)
		else
			warn("Failed to retrieve data for route: " .. entry)
		end
	end
end

1 Like

Is this being used from a localscript? if so then you cant access datastores from the client only the server. Youll have to use a remtoe function to fetch the keys from the datastore.

It’s not a local script, it’s a server sided script

This has been fixed! Thanks all for your input.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.