How to make this while loop instantly stop

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)

Hmm… is there a way to reset the function? I’m making it for a round system, where it stops when a condition is met, and then resets after a while.

you could have a check before each thing the looped function does instead then

Could you provide an example? I’m not sure what to do.

local ended = nil

start.OnServerEvent:Connect(function()
	while true do
		if not ended then
			-- do something
		end
		if not ended then 
			-- do something else
		end
		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)

Wouldn’t that still have a delay, since the while loop checks every second?

it checks ended whenever it does something in this case, even if ended == true then it’s just going to do nothing.

you wouldn’t be stopping inside the loop
you would be stopping outside it

While true loops will continue until you stop it or if it reaches a certain value. For example:

local secondsElapsed = 0 -- Live seconds
local timeout = 5 -- Seconds needed to reach
 
while true do
	print("Looping...")
	wait(1)
	secondsElapsed = secondsElapsed + 1 -- This value will increase by 1 until it reaches the specific value
 
	if secondsElapsed == timeout then -- if it reaches the value, it ends and continues down the code
		break
	end
end
 
print("Loop ended; moving on!") -- Tests to make sure it passes the loop

Reference: Loops - Roblox

that won’t work, they want the code to check all the time, not every 1 second

also you should use +=

start.OnServerEvent:Connect(function()
	while true do
-- the rest of my code
		wait(1)
	end
res:GetPropertyChangedSignal("Value"):Connect(function()
	if res.Value ~= nil or res.Value ~= "" then
		ended = true
		break;
	end
end)
end)

Maybe?

I don’t wanna be that guy lol, but i made a module that addresses this very issue with wait()'s lack of flexibility.

Source for the module: CallScheduler.lua (652 Bytes)

Usage in your case:

local scheduler = require(PathToModule)

local function ServerLoop()
    scheduler.Add(1, ServerLoop) -- add server loop to call again in 1 second from now

    -- the rest of your code
end

start.OnServerEvent:Connect(function()
	scheduler.Add(1, ServerLoop) -- start it up
end)

res:GetPropertyChangedSignal("Value"):Connect(function()
	if res.Value and res.Value ~= "" then
		scheduler.Remove(ServerLoop)
	end
end)

This should achieve what you want, but note that is not a good idea to start chains like while loops everytime single time a server event fires, you can crash the server that way.

1 Like