Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to break a while loop immediately after a value changes?

Asked by 2 years ago
local function SpawnPillars()
    while true do
        if dead == true then
            print("break")
            break
        end

        local Points = MakePillar()

        for i = 1, #Points do
            tweenPart(Points[i], 15, {Position = Points[i].Position - Vector3.new(60,0,0)}, true)
        end

        wait(waitTime)
    end
end

At the beginning of the loop I wrote an if condition to check if dead is true or not, but it only checks after the wait(waitTime) at the end, which means it only checks every 3.5 seconds. I want the loop to repeat every 3.5 seconds but I also want to break it immediately when dead is true, not after 3.5 seconds. Any solution?

0
To answer your question on my post, I believe it will break out of the for loop, however I used that method because the for loop is placed at the end of the while true do loop, meaning that the second that for loop ends the while loop will repeat. cmgtotalyawesome 1418 — 2y
0
oh thanks, i just want to tell u that i found a solution so i will use my method daokhiethy 25 — 2y

3 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

While loop runs with condition, you can do like:

local function SpawnPillars()
    while not dead do -- not dead is a condition
        wait(waitTime) -- make it wait then check so it would stop right away then dead is false
      local Points = MakePillar()
        for i = 1, #Points do
            tweenPart(Points[i], 15, {Position = Points[i].Position - Vector3.new(60,0,0)}, true)
        end
    end
end
0
that doesn't work, it's still the same. At first it checks if dead is true or not, then wait then do stuff and checks again, if dead = true and becomes false in less than the wait time the function will keep running even though the player is dead once. daokhiethy 25 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

so guys i found a solution, look something like this:

local function a()
if dead == false then
-- do stuff
wait(waitTime)

return a()

else
wait()
return a()
end

a()
Log in to vote
0
Answered by 2 years ago

Maybe you can use 2 while loop?

try this


local function SpawnPillars() spawn(function() while wait() do if dead then print(“break”) break end end end) spawn(function() while wait(waitTime) do local Points = MakePillar() for i=1, #Points do tweenPart(Points[i], 15, {Position = Points[i].Position - Vector3.new(60,0,0)}, true) end end end) end

If it still delays then maybe you can try coroutine (threading)

Im still not advanced at coroutine (I don’t really undertand it) so you can check at roblox docs https://developer.roblox.com/en-us/api-reference/lua-docs/coroutine

Im gonna learn coroutine rn (You can also learn coroutine from devking on youtube)

Answer this question