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
the reason it is not looping is because it is only looping the amount of Children is inside model
here is the fix
local model = workspace.Model while true do if #model:GetChildren() < 20 then 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
This code will accomplish what you are hoping for (making sure workspace always has at least 20 parts). Hopefully you can visualize how to add specific things to these parts on your own.
function PartAdder() local count = 0 for _,v in pairs(workspace:GetChildren()) do if v:IsA("Part") then count = count + 1 end end if count < 20 then for _ = 1,20-count do local inst = Instance.new("Part") inst.Parent = workspace end end end workspace.ChildRemoved:Connect(PartAdder) PartAdder()
Essentially what we're doing is hooking a function which will make sure workspace has at least 20 parts to the inherited ChildRemoved event of workspace. This way, whenver a child is removed, it will check to make sure that 20 parts still exist within workspace.
We then run the function once to ensure that workspace has at least 20 parts when the game starts up.
If you have questions feel free to ask, I may not have explained this super well -- but it does work how you want.
Alright, so what it seems like what you wanted was to have 20 parts created and then let the player click all of them until they were gone, and then spawn a new 20 parts, so I basically gut your entire code and rewrote it with a simpler recursive design:
local m = game.Workspace.Model function createParts(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 children = model:GetChildren() part.Parent = workspace.Model part.Position = Vector3.new(xpos,height,zpos) print(#children) wait(2) end killParts(model) end function killParts(model) while #model:GetChildren() > 0 do model.Part.ClickDetector.MouseClick:Connect(function(player) local score = player:WaitForChild("leaderstats"):WaitForChild("Score") score.Value = score.Value + 1 model.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 createParts() end createParts(m)