user4231762
user4231762

Reputation:

Error with Lua "if then" statements ('end' expected (to close 'if' at line 28))

I got an error that says

bios:14: [string "Lighting"]:58: 'end' expected (to close 'if' at line 28) 

I honestly have no idea what I'm doing since I'm new to Lua and coding in general. I assume that it has something to do with not having an end somewhere.

term.clear()
term.setCursorPos(17, 4)
print("Welcome")
sleep(2)
term.setCursorPos(8, 5)
print("What lights would you like to control?")
input = read()
if input == "Hall" then
  term.clear()
  term.setCursorPos(17,4)
  print("On or Off?")
  input = read()
  if input == "on" then
    redstone.setOutput("back", true)
    print("Hall Lighting Turned On")
    sleep(5)
    shell.run("Lighting")
  else
    redstone.setOutput("back", false)
    print("Hall Lighing Turned Off")
    sleep(5)
    shell.run("Lighting")
  if input == "Bedroom" then
  term.clear()
  term.setCursorPos(17,4)
  print("On or Off")
  input = read()
  if input == "on" then
    redstone.setOutput("left", true)
    print("Bedroom Lighting Turned On")
    sleep(5)
    shell.run("Lighting")
  else
    redstone.setOutput("left", false)
    print("Bedroom Lighing Turned Off")
    sleep(5)
    shell.run("Lighting")
  if input == "Labs" then
  term.clear()
  term.setCursorPos(17,4)
  print("On or Off?")
  input = read()
  if input == "on" then
    redstone.setOutput("right", true)
    print("Lab Lighting Turned On")
    sleep(5)
    shell.run("Lighting")
  else
    redstone.setOutput("right", false)
    print("Lab Lighing Turned Off")
    sleep(5)
    shell.run("Lighting")
  end
else
  print("Error")
  sleep(3)
  shell.run("Lighting")
end

Upvotes: 1

Views: 1034

Answers (1)

Mateusz Piotrowski
Mateusz Piotrowski

Reputation: 9127

It looks like you are missing end word in several places.

The structure should be like:

if ... then
  some code
else
  some optional code
end

Additionally, try to indent your code better. It will become clear to you where you should put end words then.

What you want is probably:

term.clear()
...
input = read()

if input == "Hall" then
  ...
  if input == "on" then
    ...
  else
    redstone.setOutput("back", false)

    shell.run("Lighting")
  end -- missing end!
end -- missing end!

if input == "Bedroom" then
    ...
  if input == "on" then
    ...
  else
    redstone.setOutput("left", false)
    ...
    shell.run("Lighting")
  end -- missing end!
end -- missing end!

...

Upvotes: 5

Related Questions