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?