This might sound a dumb question, but is there any way to break a loop outside of loop?
No, but there are other things that you can do. Make a function and then change the value of a variable to other thing inside that function, inside the while you make a conditional that whenever the variable aint the value it used to be you break it.
There are other ways such as remote functions, bindable functions.
It depends on what you’re trying to do. It’s possible to control loops in other threads by deciding the exit condition beforehand.
local run = true
spawn(function()
while (run) do
print("running")
wait(0.1)
end
end)
wait(5)
run = false
Or if you’re using value objects,
--script 1
while (workspace.SomeValue.Value == something) do
print("running")
wait(0.1)
end
--script 2
wait(5)
workspace.SomeValue.Value = somethingElse
Or with events,
local run = true
game.ReplicatedStorage.SomeBindableEvent:Connect(function()
run = false
end)
while (run) do
...
end
Hi, sorry for bumping this thread. But could you give an example?
Like, I have a while loop that I want to break, like you said, when the value don’t match anymore - the condition is not true anymore.
But, how would I end the loop from below the loop.
Like,
value = 2
while value > 1 do
print("whatever")
end
value = 0
And btw, the loop is inside a coroutine.
Hope you can help, thanks dude!
You might have already thought of this, but have you considered positioning the loop after whatever code should change the condition?
local Initialized
RemoteEvent.OnClientEvent:Connect(function()
Initialized = true
end)
repeat wait() until Initialized
Thanks for the help dude, no I didn’t think of that!
Sure thing.
30char30char30char