Jordan Bennett
Jordan Bennett

Reputation: 13

Issue with loop and nested if statements

motor = peripheral.wrap("right")

repeat 


RPM = motor.getSpeed()
print("current rpm is ", RPM, "RPM. What RPM do you need?")
setrpm = tonumber(io.read())
if type(setrpm) == "number"
    then
        motor.setSpeed(setrpm)
        goto contine

    else 
        print("Must be a number between -256 and 256")
        print("current rpm is ", RPM, "RPM. What RPM do you need?")
        setrpm = tonumber(io.read())
        end
::continue::

rpm = motor.getSpeed()
su =  ( rpm * 16)
fe = motor.getEnergyConsumption()
print("You have set speed " , rpm, " RPM")
print("Current stress cap is now ", su, "SU")
print("Power Consumption is now ", fe, "FE/t")

until( fe > 100 )
end

Expected behavor loop until fe=100 or more

current behavor motor.lua:12 '=' expected near 'continue'

writing a loop of code in computercraft to ask what rpm a block needs to spin at, expected behavor is to keep looping the code endlessly till the FE>100 (for the block its looking at its imposible)

Upvotes: 0

Views: 248

Answers (1)

Piglet
Piglet

Reputation: 28950

Computercraft uses Lua 5.1. goto was introduced in Lua 5.2 so you cannot use it in your script.

That aside there is no point in using it like that.

if condition then
  -- block A
else
  -- block B
end

After executing block A or B Lua will automatically continue after the end. You don't have to explicitly tell Lua to do that.

In a repeat until statement there is no end after the condition. The correct syntax is:

repeat block until exp

Befor posting your problems here, at least check for typos. contine is not the label you intended to go to.

Please refer to https://www.lua.org/manual/5.1/manual.html

Upvotes: 1

Related Questions