Why does my data store code keep repeating?

I’m using a data store, but the code keeps repeating, and I don’t know why, but it overloads DataStoreService. Here’s my code:

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local addGui = ReplicatedStorage.FireAddGUI


local DataStoreService = game:GetService("DataStoreService")
local passStore = DataStoreService:GetDataStore("RankStore")

Players.PlayerAdded:Connect(function(plr)
	if plr:GetRankInGroup(11635716) >= 254 then
		local success, pages = pcall(function()
			return passStore:ListKeysAsync()
		end)
		
		if success then
			local passed = {}

			while task.wait(0.1) do		
				for i, v in pairs(pages:GetCurrentPage()) do
					table.insert(passed,v)
					print("Player added to passed")
				end

				if pages.IsFinished == true then
					print("Pages finished")
					break
				end
				
				print("next page")
				pages:AdvanceToNextPageAsync()
			end	

			for i, name in pairs(passed) do
				local success, result = pcall(function()
					return passStore:GetAsync(name.KeyName)
				end)
				if success then
					addGui:FireClient(plr,name.KeyName,result)
					print("ui request sent for "..name.KeyName)
				end
				wait(0.5)
			end
		end
	end
end)

I understand that I have a repeat, but it’s repeating more than it should. I only have two keys, but it infinitely repeats. Why might this be happening? Also, how do I get rid of keys?

Probably due to this never being set to true anywhere, unless it is set in another script. Let me know if that is the case.

1 Like

Looks like this is an internal variable controlled by Roblox’s servers since it is Read Only: Properties

IsFinished

boolean

READ ONLY

NOT REPLICATED

READ PARALLEL

Whether or not the current page is the last page available.

Properties inherited from Instance

1 Like

On the doc site it does say “this iterator function may be handy”:

Pages | Roblox Creator Documentation

function iterPageItems(pages)
	return coroutine.wrap(function()
		local pagenum = 1
		while true do
			for _, item in ipairs(pages:GetCurrentPage()) do
				coroutine.yield(item, pagenum)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
			pagenum = pagenum + 1
		end
	end)
end
1 Like

Thank you for telling me. Always good to slip new information under my belt!

1 Like

Yup, same here bro. It’s new to me too, I’ll slip that under the belt too :smiley:

1 Like

Not necessarily new to scripting. I have been for six years now. I’m just self taught, so there’s still probably quite a bit of stuff I don’t even know exists yet :laughing:

1 Like

Me either, been programming for 20 years. Always good to find new stuff to learn, all day long!