I made the turtle is just for experimental reasons. The turtle should go through a small labyrinth. In order to do that, it should check: if there is not block in front it should go forward, else it should go left, but after I tried it this message popped up: l.lua:30: attempt to comparte __lt on nil and nil.
Code:
local x = 0
local y = 0
local z = 0
turtle.down()
if turtle.detect() then
local x = 1
else x = 0
end
if turtle.turnLeft() then
local y = 1
else y = 0
end
turtle.turnRight()
turtle.turnRight()
if turtle.detect() then
local z = 1
else z = 0
end
if x == y then
local a = 1 else
local a = 0
end
if x == z then
local b = 1 else
local b = 0
end
if a == b then
turtle.forward()
end
if a > b then
turtle.turnLeft()
turtle.forward()
end
if b > a then
turtle.turnRight()
turtle.forward()
end
PS. I don't have much knowledge about progamming, I just read the ComputerCraft Introductions
I need help with my turtle programm
Started by darkwarecookie, Oct 22 2018 05:33 PM
1 reply to this topic
#1
Posted 22 October 2018 - 05:33 PM
#2
Posted 22 October 2018 - 11:34 PM
The error is telling you that you're attempting to compare two incompatible values on the 30th line. "a" and "b" will be nil when the script reaches that point.
The reason for this is that you're localising them to the "if" blocks higher up, which means their values are discarded at the end of those blocks. One way around this would be to define them as upvalues - see this scope tutorial for more info on this.
You might also find this control structure tutorial handy, as at some stage you'll want to add some loops to the code.
The reason for this is that you're localising them to the "if" blocks higher up, which means their values are discarded at the end of those blocks. One way around this would be to define them as upvalues - see this scope tutorial for more info on this.
You might also find this control structure tutorial handy, as at some stage you'll want to add some loops to the code.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











