Loops that finish
LESSON 05
Build a list of clock chimes and find the first one that opens the latch.
local Script = {}function Script.run(context)
local sounds = {}
for bell = 1, 5, 2 do
sounds[#sounds + 1] = bell
end
local i = 1
while i <= #sounds do
if sounds[i] == 3 then break end
i = i + 1
end
local winding = 0
repeat
winding = winding + 1
until winding >= 2
return { position = i, turns = winding, bells = sounds }
endreturn ScriptThe numeric for includes its endpoint when the step lands there, producing 1, 3, 5. The while checks
before entering and stops at index 2. The repeat loop checks after the body, so it always runs at
least once.
Use break to leave the nearest loop. To skip an item, put the work inside if eligible then … end.
Choose a finite bound when scanning inventory. A tight loop waiting for a click prevents the runtime
from processing that click. Use a GUI action or a signal for input.
