This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mnoone96's profile picture

Lua Script Help!

Started by Mnoone96, 13 September 2015 - 02:14 PM
Mnoone96 #1
Posted 13 September 2015 - 04:14 PM
Hi There!

I wrote this script to fill a quarry and im getting pack an error in line 12 when i involve user input. Error: 'then' expected. Need Help!!
  • function placeBlockDownForward()
  • length = 0
  • – Length limit is 1 less than actual –
  • while length <= 104 do
  • turtle.forward()
  • turtle.placeDown()
  • if turtle.getItemCount() == 0 and select == 16 then
  • print "Not enough Dirt. Please place in more and hit enter 'y'"
  • re = io.read()
  • if re = "y" then
  • select = 0
  • turtle.select(select)
  • end
  • elseif turtle.getItemCount() == 0 then
  • select = select + 1
  • turtle.select(select)
  • end
  • length = length + 1
  • end
  • end
  • function placeBlockDownBack()
  • length = 0
  • – Length limit is 1 less than actual –
  • while length <= 104 do
  • turtle.back()
  • turtle.placeDown()
  • if turtle.getItemCount() == 0 and select == 16 then
  • print "Not enough Dirt. Please place in more and hit enter 'y'"
  • re = io.read()
  • if re = "y" then
  • select = 0
  • turtle.select(select)
  • end
  • elseif turtle.getItemCount() == 0 then
  • select = select + 1
  • turtle.select(select)
  • end
  • length = length + 1
  • end
  • end
  • function fullCycleReverse()
  • placeBlockDownBack()
  • turtle.up()
  • placeBlockDownForward()
  • turtle.up()
  • end
  • function fullCycle()
  • placeBlockDownForward()
  • turtle.up()
  • turtle.placeDown()
  • placeBlockDownBack()
  • turtle.up()
  • turtle.placeDown()
  • end
  • function placeBlock()
  • length = 0
  • while length <= 103 do
  • turtle.back()
  • turtle.place()
  • if turtle.getItemCount() == 0 then
  • select = select + 1
  • turtle.select(select)
  • end
  • length = length + 1
  • end
  • end
  • function turtleGoDown()
  • depth = 0
  • while depth <= 59 do
  • turtle.down()
  • depth = depth + 1
  • end
  • end
  • select = 1
  • turtle.select(select)
  • turtle.forward()
  • turtleGoDown()
  • turtle.turnRight()
  • turtle.turnRight()
  • placeBlock()
  • turtle.back()
  • turtle.place()
  • turtle.up()
  • turtle.up()
  • function fullCycleTwentySeven()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • fullCycle()
  • end
  • fullCycleTwentySeven()
Lyqyd #2
Posted 13 September 2015 - 06:40 PM
Moved to Ask a Pro.
Dog #3
Posted 13 September 2015 - 07:06 PM
On lines 10 and 31 you have

if re = "y" then

they should be

if re == "y" then

You need a double = because you are doing a comparison - a single = is used for setting a variable

Here is your code, with corrections, indented, and placed in code tags - I've place it inside a spoiler due to it's length. I also took the liberty of replacing your fullCycleTwentySeven() function with a loop, so it's easier to read and manage in the future.

Spoiler

function placeBlockDownForward()
  length = 0
  -- Length limit is 1 less than actual --
  while length &amp;amp;amp;lt;= 104 do
    turtle.forward()
    turtle.placeDown()
    if turtle.getItemCount() == 0 and select == 16 then
      print "Not enough Dirt. Please place in more and hit enter 'y'"
      re = io.read()
      if re == "y" then
        select = 0
        turtle.select(select)
      end
    elseif turtle.getItemCount() == 0 then
      select = select + 1
      turtle.select(select)
    end
    length = length + 1
  end
end

function placeBlockDownBack()
  length = 0
  -- Length limit is 1 less than actual --
  while length &amp;amp;lt;= 104 do
    turtle.back()
    turtle.placeDown()
    if turtle.getItemCount() == 0 and select == 16 then
      print "Not enough Dirt. Please place in more and hit enter 'y'"
      re = io.read()
      if re == "y" then
        select = 0
        turtle.select(select)
      end
    elseif turtle.getItemCount() == 0 then
      select = select + 1
      turtle.select(select)
    end
    length = length + 1
  end
end

function fullCycleReverse()
  placeBlockDownBack()
  turtle.up()
  placeBlockDownForward()
  turtle.up()
end

function fullCycle()
  placeBlockDownForward()
  turtle.up()
  turtle.placeDown()
  placeBlockDownBack()
  turtle.up()
  turtle.placeDown()
end

function placeBlock()
  length = 0
  while length &amp;amp;lt;= 103 do
    turtle.back()
    turtle.place()
    if turtle.getItemCount() == 0 then
      select = select + 1
      turtle.select(select)
    end
    length = length + 1
  end
end

function turtleGoDown()
  depth = 0
  while depth &amp;amp;lt;= 59 do
    turtle.down()
    depth = depth + 1
  end
end

select = 1
turtle.select(select)
turtle.forward()
turtleGoDown()
turtle.turnRight()
turtle.turnRight()
placeBlock()
turtle.back()
turtle.place()
turtle.up()
turtle.up()

function fullCycleTwentySeven()
  for i = 1, 27 do --# this will loop 27 times
    fullCycle()
  end
end

fullCycleTwentySeven()

One more thing…I noticed on lines 11 and 32 you are setting select to 0. I may be wrong, but I think turtle slots are numbered 1-16, not 0-15. Trying to select slot 0 *may* cause an error.
Edited on 13 September 2015 - 05:36 PM