0

how do I make a successful repeat and until loop? what did i do wrong in my code? I did

repeat
move forward()
until(tick{2}

)

I tried

repeat
move forward()
until{2}

)

here's the actual result: the character moved sideways only 1 space once not one space every 2 seconds. here's what i was expecting: the character moved sideways every 2 seconds

  • 2
    What game engine this script is designed for? Set the corresponding tag on your question please. – ESkri May 28 '23 at 03:44
  • 2
    `until{2}` will never terminate since tables are always truthy. We don't know what `tick` does and `move forward` is a syntax error. – Luatic May 28 '23 at 08:21

1 Answers1

1

Lua repeat has a simple Syntax

-- repeat.lua
local i = 0 -- Lets work with this
local s = 1 -- Stepsize that can be changed

repeat
  if i == 0 then -- Output only at Start
    print('Start to Repeat')
  end

  i = i + s -- Increment

-- Doing something with i
  print(type(i), i, i * i, i + i, ((i * i + i) / 2), math.rad(i))

  if i > 200 then -- Break Condition
    print('Repeat stopped') -- Output before break
    break -- Leave the Loop here
  end

  s = s + i -- Increment the Stepsize

until i == 420 -- Never reached (Because of Break Condition)

Output of above

Start to Repeat
number  1   1   2   1   0.017453292519943
number  3   9   6   6   0.05235987755983
number  8   64  16  36  0.13962634015955
number  21  441 42  231 0.36651914291881
number  55  3025    110 1540    0.95993108859688
number  144 20736   288 10440   2.5132741228718
number  377 142129  754 71253   6.5798912800186
Repeat stopped

OK - Simpliest is to have a Function to repeat that returns either true or false than...

-- Stopping when Function returns true
repeat until function_that_returns_false_or_true()

Or

-- Stopping when function returns false
repeat until not function_that_returns_false_or_true()
koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15