I did this a while back so I forgot how it worked, but my intended purpose was for the while loop to create parts until there is 20 parts in the game and stops. Then continues again when there is less than 20 parts in the game.
How can I restart the loop again?
local model = workspace.Model
while #model:GetChildren() <= 20 do
local part = Instance.new("Part")
local cd = Instance.new("ClickDetector", part)
local xpos = math.random(-100,100)
local height = 20
local zpos = math.random(-100,100)
local model = game.Workspace.Model
local children = model:GetChildren()
part.Parent = workspace.Model
part.Position = Vector3.new(xpos,height,zpos)
print(#children)
wait(2)
cd.MouseClick:Connect(function(player)
local score = player:WaitForChild("leaderstats"):WaitForChild("Score")
score.Value = score.Value + 1
part:Destroy()
if score.Value > 10 then
score.Value = score.Value + 1
end
if score.Value > 20 then
score.Value = score.Value + 1
end
end)
end

2 comments
because it is only doing the number of stuff inside model
I'm glad you asked this because I love creating recursive loops, and this is probably the perfect problem for that solution