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)
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?
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.
@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)
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)