Efficient way to break loops?

If i made a loop:
while true do

[code here]
end
What would be the most efficient way to break that loop?

Use the conditional part of the loop or use the break keyword.

3 Likes

Well since its

while true do

It will never end unless the script is completely disabled.
You could instead do something like

local count = 0
repeat 
    count = count + 1
    -your code
    wait(.1)
until count == 100

That would repeat that loop 100 times

No, please don’t, because repeat and while have different applications. This is unnecessary use of a repeat loop. incapaz’s answer is the right one here: use the conditional properly or use the break keyword to stop a loop.

local humanoid = whatever

while humanoid do -- humanoid variable is not false or nil
    someWork()
end

while true do
    if someWorkEncountersSweetSpot() then
        break
    end
end
1 Like