Leaderboard not working

I’m trying to make a leaderboard for my game to see who the top three people are with the most of the currency, but i have a problem. Once it gets to a certain part in my script it just stops. It stops after it prints “Page”. I don’t know why this is happening or how to fix it. Although i did try lowering the number where it says “GetSortedAsync(false,3)” but yeah. Any help?

wait()
for i = 1,3 do
	local clone = script.Parent.Player1:Clone()
	clone.Name = "Player"..i
	clone.Text = clone.Name
	print(i)
end
function Update()
	local ds = game:GetService("DataStoreService"):GetOrderedDataStore("Toonies")
	print("Got datastore")
	local pages = ds:GetSortedAsync(false,3)
	print("Sorted")
	local data = pages:GetCurrentPage()
	print("Page")
	for i,v in pairs(data) do
		print("Looping!")
		if tonumber(v.key) >= 1 then
			print("Key equals or is greater than 1!")
			local frame = script.Parent:FindFirstChild("Player"..i)
			if frame then
				print("Frame!")
				local player = game.Players:GetPlayerByUserId(v.key)
				frame.Text = player.Name..": "..player.leaderstats.Toonies.Value
				print("Changed!")
			end
		end
	end
end
Update()
while wait(2) do
	Update()
end

It’s because the data variable is probably nil.

Are you saving the data in the DataStore or the OrderedDataStore ? , because to get data from the OrderedDataStore you need to save the data to it.

Try doing that and it should solve your problem.

Oh yeah. I ended up testing that out and did indeed find that it was nil. I will do what you said and hopefully it works!

It works perfectly now! Thanks a lot!