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

Problem with LUA (probably a noob question)

Started by Smexyapple, 10 December 2014 - 09:20 AM
Smexyapple #1
Posted 10 December 2014 - 10:20 AM
Why is this not working?
trying to make a program that will mine 3x2 , put a torch once after 5 blocks.

I have no programing background so it will be very nice of you if you could nooby style explain it to me. Thanks!

function dig()
  turtle.dig()
  turtle.forward()
--  checkfo()
  turtle.digUp()
  turtle.turnRight()
  turtle.dig()
  turtle.up()
  turtle.dig()
--  checkup()
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.dig()
  turtle.down()
  turtle.dig()
  turtle.turnRight()
  tim = tim+1
  end
function checkfo()
  while not turtle.forward() do
    turtle.dig()
  end
end

function checkup()
  while not turtle.up() do
    turtle.digUp()
  end

function placeTo()
   turtle.up()
  checkup()
  turtle.select(2)
  turtle.placeDown()
  turtle.select(1)
  turtle.down()
end
-- Main code here
  local times = 0
  local z = 1
  term.write("How many times: ")
  times = read()
for i = 1, times do
  dig()
  z = z+1
  end
if z == 5 then
  placeTo()
  end
end
Dragon53535 #2
Posted 10 December 2014 - 04:06 PM

function checkup()
  while not turtle.up() do
	turtle.digUp()
  end
--#Add end here
function placeTo()
   turtle.up()
  checkup()
  turtle.select(2)
  turtle.placeDown()
  turtle.select(1)
  turtle.down()
end
-- Main code here
  local times = 0
  local z = 1
  term.write("How many times: ")
  times = read()
for i = 1, times do
  dig()
  z = z+1
  end
if z == 5 then
  placeTo()
  end
end--#remove end from here.
You've got your ends messed up, and everything is encased in your "checkup" function. You messed up your indentation, so it would of been easy to spot.

To view it differently. I'll properly indent what it's doing.


function dig()
  turtle.dig()
  turtle.forward()
--  checkfo()
  turtle.digUp()
  turtle.turnRight()
  turtle.dig()
  turtle.up()
  turtle.dig()
--  checkup()
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.dig()
  turtle.down()
  turtle.dig()
  turtle.turnRight()
  tim = tim+1
  end
function checkfo()
  while not turtle.forward() do
	turtle.dig()
  end
end
function checkup()
  while not turtle.up() do
	turtle.digUp()
  end
  function placeTo()
	turtle.up()
	checkup()
	turtle.select(2)
	turtle.placeDown()
	turtle.select(1)
	turtle.down()
  end
  -- Main code here
  local times = 0
  local z = 1
  term.write("How many times: ")
  times = read()
  for i = 1, times do
	dig()
	z = z+1
  end
  if z == 5 then
	placeTo()
  end
end

Spoiler

function dig()
  turtle.dig()
  turtle.forward()
--  checkfo()
  turtle.digUp()
  turtle.turnRight()
  turtle.dig()
  turtle.up()
  turtle.dig()
--  checkup()
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.dig()
  turtle.down()
  turtle.dig()
  turtle.turnRight()
  tim = tim+1
end



function checkfo()
  while not turtle.forward() do
	turtle.dig()
  end
end



function checkup()
  while not turtle.up() do
	turtle.digUp()
  end
end
function placeTo()
   turtle.up()
  checkup()
  turtle.select(2)
  turtle.placeDown()
  turtle.select(1)
  turtle.down()
end
-- Main code here
local times = 0
local z = 1
term.write("How many times: ")
times = read()
for i = 1, times do
  dig()
  z = z+1
end
if z == 5 then
  placeTo()
end
Edited on 10 December 2014 - 03:09 PM
The_Cat #3
Posted 10 December 2014 - 05:07 PM

function placeTo()
  turtle.up()
  checkup()
  turtle.select(2)
  turtle.placeDown()
  turtle.select(1)
  turtle.down()
  z = 0 --Here
end
if z == 5 then
  placeTo()
  z = 0 --Or Here (You dont need to put it at both locations)
end

local z = 0 – Make it 0 so it actual has 5 spaces between the torches too.

I haven't actual used the code in game but by looking at the placeTo() you will probably want to add at the end z = 0 Because if you don't add z = 0 then it will just keep adding on to 5 so z will never equal 5 again. Also seeing the placeto function the turtle wont be able to go down becuase the torch will block it, you could change the function to this:

function placeTo()
  turtle.digDown()
  turtle.select(2)
  turtle.placeDown()
  turtle.select(1)
end
Edited on 10 December 2014 - 04:08 PM
Smexyapple #4
Posted 10 December 2014 - 06:00 PM
Thanks for the help guys. i've looked back on my code after posting this and spotted alot of mistales before the thread was even published . ill use you'r advises to perfect my code and hopefully ill learn alot from it.