How to make this while loop instantly stop

I’m having this issue: I have a while true loop that repeats every second. However, I want it to instantly break when something happens. (in this case - a variable change)

How would I accomplish this?

Code:

start.OnServerEvent:Connect(function()
	while true do
-- the rest of my code
		wait(1)
	end
end)

res:GetPropertyChangedSignal("Value"):Connect(function()
	if res.Value ~= nil or res.Value ~= "" then
		ended = true
		-- I want the while loop above to instantly stop here
	end
end)

Thanks.

1 Like

nest the second section into the while loop before the wait(1)

How would that allow me to instantly stop the loop?

start.OnServerEvent:Connect(function()
	while true do
		if res.Value ~= nil or res.Value ~= "" then
			ended = true
			break
		end
		wait(1)
	end
end)

use the global variable “break”

4 Likes

you can make the loop a coroutine and stop the coroutine when you need it to be stopped

3 Likes

Can you give me an example? I’m not too familiar with coroutines.

@D0RYU @QueriesFromADumbDev
Wouldn’t that only check once a second? I want it to instantly happen.

using “break”
will instantly stop the loop from running ever again

I meant that I want the loop to instantly stop when the variable is set to true. Wouldn’t that code make it check once a second, allowing time for delay?

while true do
    if true then
        break
    end
end

that has no yielding so running that would freeze studio
but it is an example

you can use break in for loops and repeat loops as well

1 Like

Inside the statement where you want the loop to stop, add a break. Alternatively, you can put the reverse of your statement right after while, because a while loop is a statement checker after all. Or use a repeat until loop.

I know, but I have a wait(1) inside the loop. If the variable is changed during the wait, it will not instantly react. I want the while true loop to instantly react when it is changed.

if you need to run it again after using break then use a function and call it whenever

or you can use coroutines to create a new thread which can be stopped and ran again easily, this is what @MamaGobies said

Are you able to give an example of how to use coroutines?

use coroutines then, that would be much better then using break then

these are very helpful links, they explain things much better then I can

@D0RYU How would I stop the coroutine (and am I using it correctly)

local function round()
	while true do
		-- stuff
		wait(1)
	end
end

local thread = coroutine.create(round)

start.OnServerEvent:Connect(function()
	coroutine.resume(thread)
end)

res:GetPropertyChangedSignal("Value"):Connect(function()
	if res.Value ~= nil or res.Value ~= "" then
		ended = true
		-- how to stop?
	end
end)

use coroutine.yield() to pause the coroutine
and use coroutine.resume() to resume the coroutine when it is paused

local function round()
	while true do
		-- stuff
		wait(1)
	end
end

local thread = coroutine.create(round)

start.OnServerEvent:Connect(function()
	coroutine.resume(thread)
end)

res:GetPropertyChangedSignal("Value"):Connect(function()
	if res.Value ~= nil or res.Value ~= "" then
		ended = true
		coroutine.yield(thread)
	end
end)