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

Simple conditional statement error

Started by Monkeycoder, 10 December 2013 - 03:46 AM
Monkeycoder #1
Posted 10 December 2013 - 04:46 AM
I've just started Lua and have pretty much no experience with any other code…

The rest of the code: http://pastebin.com/2bqiracJ (it isnt fully done..)

This is the code im making for a custom size builder, but:

for i = 1, howMany do
Place1()
if i == howMany then
turtle.up(1)
end
end

It is not moving up when it is down, it is continuing straight to:

for h = 1,howMany2 do
Place2()
end

which does the 2nd row

The rest of the code: http://pastebin.com/2bqiracJ (it isnt fully done..)
Edited on 10 December 2013 - 05:37 AM
Engineer #2
Posted 10 December 2013 - 09:06 AM
read() returns a string, not a number. Luckily we have a function to convert a string to a number, but when the string is not a number, then it returns nil.
Thus, we can convert it directly to a number, but we need to check if the value is not nil:

term.write("How many across?"..": ")
local howMany = tonumber(read())
print("You entered: "..howMany)
term.write("How high?"..": ")
local howMany2 = tonumber(read())
print("You entered: "..howMany2)

if howMany and howMany2 then
  -- continue the script
end
Though, I have a little snippet which keeps asking for a number, this is purely for code, I dont care about the graphical appearance. That one is for you:

local howMany
repeat
    howMany = tonumber(read())
until howMany 
You get the point
awsmazinggenius #3
Posted 10 December 2013 - 09:28 AM
Also, turtle.up() takes no parameters.

turtle.up() --# Makes the turtle go up one block

for i=1, 5 do --# Makes the turtle go up 5 blocks
  turtle.up()
end