Stop a loop based on an event

Hello,

Let’s say I have this loop

while true do
   --random code
   wait(15)
   --different code
   wait(15)
end
´´´

How can I make it so at any time, if the event happens it will instantly stop? Event being :GetPropertyChangedSignal()

Thank you in advance

Do you need it to stop midway through the loop, or is it fine if it completes another iteration? If it’s fine to complete another iteration, you could set a variable to true, use that as the condition in the while loop, and only set it to false on :GetPropertyChangedSignal. Quick example:

local notFired = true

game:GetPropertyChangedSignal[[ChildAdded]]:Connect(function()
	notFired = false
	-- something else maybe
end)

while notFired do
	--random code
	wait(15)
	--different code
	wait(15)
end
2 Likes

It needs to stop right away, can’t wait until it finishes the iteration

You could put the loop in a script, and then disable it when you need to.

If I renable it right away, will it reset the script? like reset all variables, restart all loops, etc

It will be reset, yes.
(30 characters)