Need help modifying quest system to handle variable number of active quests

Hello Devs,

Im working on a quest system where players can have up to 3 active quests at a time. Currently, my code works fine when there are exactly 3 active quests, but breaks when there are fewer 1 or 2. Id appreciate any help on modifying this code to handle any number of active quests 1-3 without breaking.

local replicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = replicatedStorage:FindFirstChild("QuestEvent")
local MainFrame = script.Parent.Parent.Frame

local tweenservice = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 0,false )

local startPos = {
	Position = UDim2.new(0.369,0, 1.151, 0)
}

local activeQuest = {}

MainFrame.Position = UDim2.new(0.369,0, 1.151, 0)
MainFrame.Visible = false

--// COUNT QUESTS
local function countActiveQuests()
	local count = 0
	for _ in pairs(activeQuest) do
		count = count + 1
	end
	return count
end

--//UPDATING QUEST GUI

RemoteEvent.OnClientEvent:Connect(function(arg,name, goal, reward, progress, rewardtype,isCompleted)
	if arg == "UPDATE" then
		local quest = activeQuest[name]
		
		if quest then
			quest.Progress = progress
			quest.isCompleted = isCompleted
			
			print(activeQuest[name])
			print(quest.isCompleted)
		end
	elseif arg == "NEW" then
		if not activeQuest[name] then
			local info = {
				isReserved = nil,
				Name = name,
				Goal = goal,
				Reward = reward,
				Progress = progress,
				RewardType = rewardtype,
				isCompleted = isCompleted
			}

			activeQuest[name] = info

			print(activeQuest)
			print("Daily Quest:", name, "- Progress:", progress .. "/" .. goal)
		end
	end 
end)

--// WAIT UNTIL THERE ARE 3 ACTIVE QUESTS

repeat
	task.wait()
until countActiveQuests() == 3



if  countActiveQuests() == 3 then
	
	local assignedQuests = {} -- Keep track of assigned canvases

	for _, quest in pairs(activeQuest) do
		print("Checking quest:", quest.Name, quest.Reward, quest.RewardType)

		for _, canvas in pairs(MainFrame:GetChildren()) do
			if canvas:IsA("CanvasGroup") and not assignedQuests[canvas] then
			
				assignedQuests[canvas] = quest
				quest.isReserved = canvas.Name

				print("Updating client for quest:", quest.Name, " -> Canvas:", canvas.Name)

				local titlelabel = canvas:FindFirstChild("Title")
				local rewardlabel = canvas:FindFirstChild("Reward")
				local button = canvas:FindFirstChild("ClaimReward")
				local progressBar = canvas:FindFirstChild("ProgressBar")

				if titlelabel and rewardlabel and button and progressBar then
					titlelabel.Text = quest.Name
					
					--// KEEP UPDATING PROGRESS BAR
					
					coroutine.resume(coroutine.create(function()
						while quest.isCompleted ~= true do
							task.wait(0.01)
							progressBar:FindFirstChild("Progress").Text = tostring(quest.Progress) .. "/" .. tostring(quest.Goal)
							local bar = progressBar:FindFirstChild("Bar")
							bar.Size = UDim2.new((quest.Progress / quest.Goal), 0, 1, 0)
						end
					end))

					rewardlabel.Text = "+" .. tostring(quest.Reward) .. " " .. tostring(quest.RewardType)

					if not quest.isCompleted then
						button.GroupColor3 = Color3.fromRGB(54, 54, 54)
					else
						button.GroupColor3 = Color3.fromRGB(255, 255, 255)

						local textButton = button:FindFirstChild("TextButton")
						if textButton then
							textButton.MouseButton1Click:Connect(function()
								textButton.Text = "CLAIMED"
								local event = replicatedStorage:FindFirstChild("ClaimEvent")
								if event then
									event:FireServer()
								end
								activeQuest[quest.Name] = nil
								button.GroupColor3 = Color3.fromRGB(54, 54, 54)
							end)
						end
					end
				end
				break 
			end
		end
	end
else
	print("There are " .. countActiveQuests() .. " active quests.")
end

--// CLOSE BUTTON

script.Parent.CloseButton.MouseButton1Down:Connect(function()
	local tween = tweenservice:Create(MainFrame, tweeninfo, startPos)
	tween:Play()

	if MainFrame.Position.Y.Scale == 1.151 then
		MainFrame.Visible = false
	end
end)

I’ve tried changing this code :

repeat
	task.wait()
until countActiveQuests() == 3

to this :

local maxWaitTime = 2
local elapsed = 0
local x = 0.1

while countActiveQuests() <= 3 and elapsed < maxWaitTime do
	task.wait(x)
	elapsed += x
end

idk what i did here, but i tried to check if the count stops at 2 or less for 2 seconds then it just break the loop and proceed to the next line. It didn’t fix my issue tho instead made the entire script not working i guess ? No errors in the ouput.