How do I make scripts stop repeating if there is too many parts spawned in?

I currently have a game that spawns in parts every second, with the parts that spawn it you have to climb to the top. Now the problem here is that if nobody reaches the top it’ll overflow and make it impossible. How would I be able to fix that and how would I make it reset that script after the round is over?

1 Like

Use break and statements to stop the loop.

1 Like

Well, you can try to add a number value to the script, with the set number of part needed to get there, and also try using a bool to check for the script being reset.

I can see how break would work however how would I make it reloop if the requirements are met again?

Simply create a function for the loop.

local Running = true
local PartLimit = 100

local SetState ="BindableEvent", script)
SetState.Name = "SetState"
	if not type(State) == "boolean" then return end
	Running = State

	local PartCount = 0
	while true do
		while Running and PartCount < PartLimit do
			local Part ="Part", workspace)
			PartCount += 1
			Part:GetPropertyChangedSignal("Parent"):Connect(function() if Part.Parent == nil then PartCount -= 1 end end)

I feel like this would actually be a decent use of a coroutine. By having it yield() its self, you could then have it resume when ever you needed it to generate more. You could then clear the parts and recreate the coroutine to start the next round.

I found a link to a pretty good tutorial!

Parent all of those parts inside a folder, and use a while loop to check constantly if there are more parts than you want

Edit :

while #Folder:GetChildren() < 10 do

use break like

for i = 10,1,-1 do
      if i == 5 then