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

Program Problem

Started by M4WL, 16 November 2013 - 09:48 AM
M4WL #1
Posted 16 November 2013 - 10:48 AM
Hey,
I'm new to lua programming and have tried everything i can to get this code working, whenever i iniate the startup command it just goes on to the next line without doing anything, the code is below:

function refuel() --ReFuels the turtle if the fuel count is less than 200.
if turtle.getFuelLevel < 200 then
    turtle.refuel()
    print("No Fuel Left, Refuelling...")
    else
	 print("No Fuel Needed")
  end
end
function buildPlatform() --Specifies the blocks to build up and iniates the building sequence.
  local build = ""
 
  term.write("How Many Blocks Up? ")
build = read()

for i = 1, build do
while not turtle.up() do
  turtle.digUp()
end
end
function moveDown()
build = read()
turtle.down()
print("Done, Thank you for using the simple platform builder!")
end
--Main Program Below
 
   print("Welcome To The Simple Platform Builder!")
   print("The system will now do everything required to start,")
    print("Checking fuel level... ")
	   refuel()
	   print("Initializing build printer...")
	    print("Initialization Complete, Enjoy your Program!")
	   print("Please Remember to place a block in slot 2, or the program WILL break.")
	   buildPlatform()
	   turtle.select(2)
	    turtle.forward()
	    turtle.place()
	    turtle.up()
	    turtle.forward()
	    turtle.turnLeft()
	    turtle.forward()
	    turtle.placeDown()
	    turtle.forward()
	    turtle.placeDown()
	    turtle.turnRight()
	    turtle.forward()
	    turtle.placeDown()
	    turtle.forward()
	    turtle.placeDown()
	    turtle.turnRight()
	    turtle.forward()
	    turtle.placeDown()
	    turtle.turnRight()
	    turtle.forward()
	    turtle.placeDown()
	    turtle.forward()
	    turtle.forward()
	    turtle.turnLeft()
	    turtle.turnLeft()
  moveDown()
  end

Please help me! Thanks in advance.
Bomb Bloke #2
Posted 16 November 2013 - 05:12 PM
That "end" statement at the bottom there is "ending" your "buildPlatform()" function declaration. Because you have no code that's outside your function declarations, nothing happens, so that "end" will need to be moved to up above the "moveDown()" declaration.

This would've been rather more obvious to you if you'd indented your code correctly. Every time you write a line that's going to need a corresponding "end"/"else"/"elseif" statement (eg, "function"/"if"/"while"/etc), all lines underneath should be indented a bit further to the right until you reach that statement. Keep everything lined up and you'll never get confused as to where such statements need to go - the last line of your program should never be indented.

Also bear in mind that the currently turtle's currently selected slot typically does not change unless you change it. "turtle.refuel()" only tries to get fuel from that slot.

"read()" returns a string, which cannot be treated as a number. It can be converted to a number, though - try something like "tonumber(read())". Note that it'll return as nil if the string isn't suitable (eg, someone typed "cabbage"), and for the purposes of an "if" or "while" statement, nil means the same thing as false.
M4WL #3
Posted 17 November 2013 - 05:42 AM
Thanks bomb, really helped.