Attempt to index nil with 'GetCurrentPage' Error

local DataStoreService = game:GetService("DataStoreService")
local BadgeDataStore = DataStoreService:GetDataStore("BadgeDataStore")




local function PrintAllKeys()
	local success, keyPages = pcall(function()
		return BadgeDataStore:ListKeysAsync("")
	end)

	if success and keyPages then
		local keys = keyPages:GetCurrentPage()
		while #keys > 0 do
			keys = keyPages:GetCurrentPage()
			for _, key in ipairs(keys) do
				local keyName = key.KeyName
				local valueSuccess, value = pcall(function()
					return BadgeDataStore:GetAsync(keyName)
				end)
				if valueSuccess then
					makerankings(value)
				else
					warn("Failed to get value for key:", keyName)
				end
			end
			if keyPages.IsFinished then
				break
			else
				keyPages = keyPages:AdvanceToNextPageAsync()
				
			end
		end
	else
		warn("Failed to list keys:", keyPages)
	end
end

PrintAllKeys()

here i keep getting the error attempt to index nil with ‘GetCurrentPage’ at
local keys = keyPages:GetCurrentPage()
and i dont know why can someone help me on this?

AdvanceToNextPageAsync() does not return anything. Instead, you should simply call it if pages.IsFinished == false

You should also refer to the documentation on the proper usage on looping GetCurrentPage(): Pages | Documentation - Roblox Creator Hub