hello guys, i’ve been wondering is there a such better way to make a while loop
there’s only way to stop a while loop is a condition(“if statements” or if a value is true or not),
its either at the start of the loop or inside of it
the only problem i have with using conditions to stop a while loop is,
local condition = false
while condition == true do -- while condition is true
print("first") --prints immediately
wait(10)
print("last") --waits to be printed
end
for this loop if you change the condition to false while the loop is still running it will still print “last”
it’ll only stop once it reaches end and the condition is false
its the same when you have the condition inside the loop
while true do
print("first")
wait(10) --has to wait until it reaches the condition to be able to stop
print("last")
if condition == false then break end -- either return or break does the same thing to stop the loop
end
for this loop it also has to wait until it reaches the condition to stop running
and there is also one other loop
the repeat until, its also does basically the same thing such as the other loops
repeat
print("first")
wait(10)
print("last")
until condition == false
there’s also another problem with loops
the code underneath the loop won’t execute
while true do
print("hey")
wait(10)
end
print("done!") -- doesn't print
so what do all these loops have in common?
-
well first of all if you put code underneath the loop, the code doesn’t execute
-
second, there has to be a condition until the loop can be able to stop, meaning it has to wait until the script reaches to the condition to be able to be stopped
-
and lastly to me it just gives me a headache using these loops and a reckoning of an unstoppable force
I tried to fix these problems by using runserivce into the mix, but im not too sure if this is the best method of making an alternative while loop, im just wondering is there a better way to solve this problem?
this is what i tried doing
local RunService = game:GetService("RunService")
local cooldown = false
local function loop()
if cooldown == true then return end --debounce/cooldown
cooldown = true
print("hi")
task.wait(1)
cooldown = false
end
print("hello")
local loopConnect = RunService.Heartbeat:Connect(loop) --be able to make a connection
task.wait(10)
loopConnect:Disconnect() --disconnecting the connection
thanks for reading this, and please help spare the pain