Actually the most simple way to do so is to add another script that disables your loop script, which means you have to seperate the two functions into two scripts(one for the loop and another for the break) and you’ll be able to instant disable the loop script aka break the loop with another break script outside your loop. This also won’t have the wait(1) issue as the break is seperate outside your script.
You can change it by adding a
repeat until
Function to your code
Try this, I’m sure someone here has already said it:
local running = true
local loop = task.spawn(function() -- your loop is in this thread
while running do
--something
end
end)
event:Connect(function()
running = false -- should stop the loop.
task.cancel(loop) -- should also stop the loop, although this one should be INSTANT.
end)
A bit pedantic of me but “break” is a keyword not a global variable
Maybe use an internal wait() on a smaller interval and accumulate the values. It’s not a 100% accurate so will be a few milliseconds out but it works.
start.OnServerEvent:Connect(function()
while true do
if ended == true then wait(1);
else
-- the rest of my code
local elapsed = 0;
while ended == false and elapsed < 1 do
elapsed += wait();
end
print("loop stopped after " .. elapsed);
end
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)