Loop doesn't work

Hello, I’m trying to make a ordered data store. But, it doesn’t print out anything.

	--Give Data
	local sucess, err = pcall(function()
		local page = cashlist:GetCurrentPage()

		for plrRank, data in ipairs(page) do
			print(data.key, player.UserId, data.Value) -- Doesn't print
			if tonumber(data.key) == tonumber(player.UserId) then
				print("A")
			end
		end

		local advanced, err = pcall(function()
			page:AdvanceToNextPageAsync()
		end)
	end)
end

Thank you.

Where are the data and player variables or parameters?

The player is

game:GetService("Players").LocalPlayer

And the data is part of the loop like

for i, v in pairs()

You cannot get datastore data on the client, you will have to do this on the server.

1 Like

Oh shoot. I gave you the wrong information I’m sorry. I thought I did, but I actually did a player added event on the sever. Sorry!
:person_facepalming::person_facepalming::person_facepalming:

I edited the script. This is where I’m at.

--Give Data
	local sucess, err = pcall(function()
		local page = cashlist:GetCurrentPage()

		for plrRank, data in ipairs(page) do
			print(data.key, player.UserId, data.Value)
			if tonumber(data.key) == tonumber(player.UserId) then
				print("A")
			else
				
				if page.IsFinished then
					break
				end
				
				page:AdvanceToNextPageAsync()
			end
		end

also it prints sucess

Is this a glitch I basically used this code found in the devhub:
https://developer.roblox.com/en-us/api-reference/class/OrderedDataStore
Edit: Whole script btw

--Variables
local Players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local cashData = DataStoreService:GetOrderedDataStore("CashData")


--Player Added Function
function onPlayerAdded(player)
	--leaderstats
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	--Give Data
	local sucess, err = pcall(function()
		local cashlist = cashData:GetSortedAsync(false, 20)
		local page = cashlist:GetCurrentPage()

		for plrRank, data in ipairs(page) do
			print(data.key, player.UserId, data.Value)
			if tonumber(data.key) == tonumber(player.UserId) then
				print("A")
			else
				
				if page.IsFinished then
					break
				end
				print("j")
				page:AdvanceToNextPageAsync()
			end
			
		end
	end)
end

-- On Player Removing
function onPlayerRemoving(player)
	local leaderstats = player:WaitForChild("leaderstats")
	
	--Set Data
	local sucess,err = pcall(function()
		local PlayerUserID = 'Player_'..player.UserId
		cashData:UpdateAsync(PlayerUserID, leaderstats:WaitForChild("Cash").Value)
	end)
end

-- On Bind To Close
function onBindToClose()
	for _, player in pairs(Players:GetChildren()) do
		local leaderstats = player:WaitForChild("leaderstats")
		
		--Set Data
		local sucess,err = pcall(function()
			local PlayerUserID = 'Player_'..player.UserId
			cashData:UpdateAsync(PlayerUserID, leaderstats:WaitForChild("Cash").Value)
		end)
	end
end

-- Connect Functions
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
game:BindToClose(onBindToClose)

Hello I still need help this is my current script:

-- Variables
local DataStoreService = game:GetService("DataStoreService")
local cashData = DataStoreService:GetOrderedDataStore("CashData")

local Players = game:GetService("Players")

function onPlayerAdded(player)
	
	-- Create leaderstats
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	-- Give Data
	local sucess, err = pcall(function()
		local pages = cashData:GetSortedAsync(false, 10)
		local currentPage = pages:GetCurrentPage()
		
		for rank, data in ipairs(currentPage) do
			if data and data.Key and data.Value then
				if data.Key == player.UserId then
					print(data.Key, data.Value)
				end
			else
				print("No Data")
			end
		end
	end)
	
	if not sucess then
		warn(err)
	end
end

function onPlayerRemoved(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local cash = leaderstats:WaitForChild("Cash")
	
	local sucess, err = pcall(function()
		cashData:SetAsync(player.UserId, cash.Value)
		print("Saved Data!")
	end)
	
	if not sucess then
		warn(err)
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoved)