local clickdetector = --somewhere it doesnt matter where
local stop= false
while stop = false do
print("hi")
end
clickdetector.Mouseclick:Connect(function()
stop=true
end)
Now as far as we know the code under this while loop will nevee run as long as stop is true so in this example the code under while loop is an event of a mouse click which will change stop into true thus stops the while loop so does this event count as a new thread or what because how could it the event fire if the current thread cant reach it
Events don’t yield the current thread, so you can put the event prior to the while loop and have everything work as intended.
For your example:
local clickdetector = --somewhere it doesnt matter where
local stop= false
clickdetector.Mouseclick:Connect(function()
stop=true
end)
while stop = false do
print("hi")
end
That (sequencing) will work fine ↑
You can actually slightly improve the code, by replacing Connect with Once, so the memory of the event is freed up.
If you ever need an event to yield the script, then you can do Event:Wait() instead of Event:Connect(…).
While loops yield a script until the logic is successful, so the event is never connected until after the value of stop is set to true.
looking at the code further, there’s actually a couple of other issues (= instead of == for the comparison, clickdetector.Mouseclick should be clickdetector.MouseClick, and no wait in the loop (script timeout will occur))
So the fully functional code would be:
local clickdetector = --somewhere it doesnt matter where
local stop= false
clickdetector.MouseClick:Connect(function()
stop=true
end)
while stop == false do -- OR while not stop do
print("hi")
task.wait() -- or wait()
end