How do I make this loop stop, then play?

How do I make this loop stop, then play every few seconds? Can someone fix this script?

local amount = 1000


local person = script.Parent.Parent
local cam = game.Workspace.CurrentCamera

repeat wait() until game.Workspace:findFirstChild(person.Name)~=nil
repeat wait() until game.Workspace:findFirstChild(person.Name):findFirstChild("Head")~=nil
local theperson = game.Workspace:findFirstChild(person.Name)

local head = game.Workspace:findFirstChild(person.Name):findFirstChild("Head")


local part = Instance.new("Part")
part.Anchored = true
part.CanCollide = false
part.Transparency = 0.5
part.BrickColor = BrickColor.new("Pastel light blue")
part.TopSurface = "Smooth"
part.BottomSurface = "Smooth"
part.formFactor = "Custom"
part.Size = Vector3.new(.2,1.2,.2)
local mesh = Instance.new("BlockMesh")
mesh.Scale = Vector3.new(.4,2,.4)
mesh.Parent = part
wait()

while true do
for i = 1, amount do
	wait(.03)
	local drop = part:Clone()

	function onHit(hit)
		drop:Remove()
end

drop.Touched:connect(onHit)
	drop.Parent = cam
	drop.CFrame = cam.CoordinateFrame *CFrame.new(math.random(-50,50),math.random(80,110),math.random(-50,50))
	drop.Anchored = false
end
end

This is the part I want to modify:
image

The script goes in StarterGui Btw.

2 Likes

I dont see any script, maybe you pasted it wrong

1 Like

Sorry about that! Here’s the script!

1 Like

You can have a control variable outside of the loop like for example called paused and then in the loop you can have all of that loop code wrapped in an if which checks too see if paused isnt true and if it is then set it to false every few seconds

1 Like

first you can create an anonymous function, which means you don’t make a separate function for the hit event

part.Touched:Connect(function(hit)
    --this will run when the part is touched
end)

next I would remove the while or for loop as it isn’t necessary.

for i=1, amount do
    --runs a set amount of times
end

or

while spawnParts do
    --runs indefinitely with a cooldown, without the cooldown the game will crash
    task.wait(cooldown)
end

you also forgot to parent the clone, which should be the last thing you do, as setting the properties while the part isn’t parented takes less memory

1 Like

What should the script look like then?

1 Like
local part = workspace.Part
local amount = 50
local pause = 5

while true do
	for i = 1, amount do wait(.03)
		local drop = part:Clone()
	     -- whatever you're doing here
	end

	task.wait(pause)
end
1 Like

Soo, the problem is, you can’t stop the loop and then restart it without restarting loop from start, but if we have while loop, that have if condition inside of it, you can change condition to false, soo while loop will run, but the condition wiill block the function, then if you wan’t, set the value to true and then loop start, if you wan’t to break loop, set the main condition to false, soo loop will break permanently