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

Do while loops repeat again after breaking?

Asked by 6 years ago

I have a question. So you know how while true do loops always go on forever? What happens after you break the loop? Does the loop start again once the right conditions are met?

For example,

local plr = game.Players.LocalPlayer
local char = plr.Character
local torso = char.Torso

while char.Torso.Position.Y > 0 do

    print("Above 0 studs")

    wait()
else
    if char.Torso.Position.Y <= 0 then break end
end

In this script, once the player is below 0 studs in terms of the Y-axis, the loop should break. But will the loop repeat and go again while the player is above 0 studs?

0
No, it will not. User#25115 0 — 6y
0
If you want this to work, you may want to use an event. User#25115 0 — 6y
0
"break end" shouldn't be a valid way to end a loop DeceptiveCaster 3761 — 6y
0
"break" and "end" should be in separate lines DeceptiveCaster 3761 — 6y
View all comments (3 more)
0
doesn't matter whether they are on seperate lines or not, why is there a random else? Vulkarin 581 — 6y
0
One of those two conditional checks aren't needed either. xPolarium 1388 — 6y
0
you can't do else if just do else or elseif HappyTimIsHim 652 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Edit: You are using a while statement incorrectly the while statement does not have an else available for it: http://www.lua.org/pil/4.3.2.html

The while statement will continue until the argument given is false, so essentially the code AFTER the while loop is the else (no else statement needed)

The break command in lua will end a loop and continue to the following code after the loop. Example from roblox wiki are:

while wait() do
    print("Hi mom!")
    break  -- This forces the endless loop to end
end

and

for i = 1, 1000000000 do
    print("Hi mom!")
    wait()
    break  -- This forces the ridiculously long loop to end
end

"Both of these loops only run once because of the break command, and print “Hi mom!” once."

cited from: https://developer.roblox.com/articles/Roblox-Coding-Basics-Loops#Break

P.S you don't need to have the second if statement inside of your else you can just put:

local plr = game.Players.LocalPlayer
local char = plr.Character
local torso = char.Torso

while char.Torso.Position.Y > 0 do

    print("Above 0 studs")

    wait()
else
    break
end

Hope this helps!

Ad

Answer this question