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 = Instance.new("BindableEvent", script)
SetState.Name = "SetState"
SetState.Event:Connect(function(State)
	if not type(State) == "boolean" then return end
	Running = State
end)


coroutine.wrap(function()
	local PartCount = 0
	while true do
		wait(0.5)
		while Running and PartCount < PartLimit do
			wait()
			local Part = Instance.new("Part", workspace)
			PartCount += 1
			Part:GetPropertyChangedSignal("Parent"):Connect(function() if Part.Parent == nil then PartCount -= 1 end end)
		end
	end
end)()
3 Likes

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
    --code
end

use break like

for i = 10,1,-1 do
      wait(1)
      if i == 5 then 
           break
      end
end