Jump to content




Simple conditional statement error


  • You cannot reply to this topic
2 replies to this topic

#1 Monkeycoder

  • New Members
  • 1 posts

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 by Monkeycoder, 10 December 2013 - 06:37 AM.


#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

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

#3 awsmazinggenius

  • Members
  • 930 posts
  • LocationCanada

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






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users