While loop going too early

So I have been trying to make a while loop only happen when a number variable reaches the value of ‘2’. However, as soon as one person joins my queue, the loop immediately happens. The code to the system is below.

--// Services \\--

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RunService = game:GetService("RunService")

--// Variables \\--

local Queue = ReplicatedStorage:WaitForChild("Queue")
local countdown = ReplicatedStorage:WaitForChild("Countdown")

local roundStarted = ReplicatedStorage:WaitForChild("RoundStarted")

local spawns = {
	[workspace.Spawns.P1] = {false},
	[workspace.Spawns.P2] = {false},
}

--// Remotes \\--

local Remotes = ReplicatedStorage:WaitForChild("Events")
local AddQueue = Remotes:WaitForChild("AddToQueue")
local RemoveQueue = Remotes:WaitForChild("RemoveQueue")
local StartRound = Remotes:WaitForChild("StartRound")

--// Remote listeners \\--

AddQueue.OnServerEvent:Connect(function(player)
	local sv = Instance.new("StringValue")
	sv.Name = player.Name
	sv.Parent = Queue
end)

RemoveQueue.OnServerEvent:Connect(function(player)
	if Queue:FindFirstChild(player.Name) then
		Queue:FindFirstChild(player.Name):Destroy()
	end
end)

local numChildren = 0

Queue.ChildAdded:Connect(function()
	numChildren = numChildren + 1
end)

Queue.ChildRemoved:Connect(function()
	numChildren = numChildren - 1
end)
--// Functions \\--

local function spawnPlayer(player)
	for i,v in pairs(spawns) do
		if v[1] == false then
			v[1] = true
			player.Character:MoveTo(i.Position)
			break
		end
	end
end

local function ResetSpawns()
	for i, v in pairs(spawns) do
		if v[1] == true then
			v[1] = false
		end
	end
end

--// while loops \\--

local function startIntermission()
	while wait() do
		if numChildren > 2 then
			break
		else
			return
		end
	end
end

startIntermission()

while roundStarted.Value == false do
	repeat wait() until numChildren > 2
	
	startIntermission()
	
	roundStarted.Value = true
	
	local Queued = Queue:GetChildren()
	
	local selected1 = Queued[math.random(#Queued)]
	local selected2 = Queued[math.random(#Queued)]
	
	local p1 = game.Players:FindFirstChild(selected1.Name)
	local p2 = game.Players:FindFirstChild(selected2.Name)
	
	Queue:FindFirstChild(p2.Name):Destroy()
	Queue:FindFirstChild(p1.Name):Destroy()
	
	StartRound:FireClient(p1)
	StartRound:FireClient(p2)
	
	spawnPlayer(p1)
	spawnPlayer(p2)
	
	for i = 60, 0, -1 do
		countdown.Value = i
		wait(1)
	end
	
	p1:LoadCharacter()
	p2:LoadCharacter()
	
	ResetSpawns()
	
	roundStarted.Value = false
end

Can anyone help?

Is anyone going to help me out here?

in your while wait there is no conditional until after. I’m fairly new to scripting but perhaps try doing; “while numChildren > 2 do” instead of you situation right now.

Where do you recommend I put that, as I have tried putting that in the loop or made the loop a condition of it, and neither worked

Hmm, I will let the next person respond, I am not very experienced with scripting that is the only idea I could think of

I see you are using ChildAdded here, this counts for all instances that are loaded into wherever you are calling this event. So when things are loaded in ReplicatedStorage it is counting that to your variable “numChildren.” Are you trying to wait for something specific?

Thanks man, but I figured it out on my own. It had nothing to do with that! I appreciate knowing that tho, as I forgot about that before!