Turtle/CC Lua Code not working

  • Please make sure you are posting in the correct place. Server ads go here and modpack bugs go here
  • The FTB Forum is now read-only, and is here as an archive. To participate in our community discussions, please join our Discord! https://ftb.team/discord

Haddock

New Member
Jul 29, 2019
5
0
0
Any help with this would be appreciated. Apparently there is an error with comparing strings and numbers and receiving strings in the " while r<a do" line but I'm not sure how to fix it. I am inexperienced in general with Lua.

Code:
term.clear()
term.setCursorPos(1,1)
 
 
 
print("Length (1st and 3rd side)")
x = read()
print("Width (2nd and 4th side)")
y = read()
 
 
turtle.select(1)
 
function breakPlaceMove()
if turtle.detectDown() then
    turtle.digDown()
    turtle.placeDown()
    turtle.forward()
end
end
 
function runTime(a)
r = 1
while r<a do
  breakPlaceMove()
r = r + 1
end
end
 
runTime(x)
 
turtle.turnLeft()
 
runTime(y)
 
turtle.turnLeft()
 
runTime(x)
 
turtle.turnLeft()
 
runTime(y)

Thanks so much guys.
Love the FTB community :)
All help appreciated!!!
 

Peppe

New Member
Jul 29, 2019
836
0
1
What does your read function return? Is it sometime not a number?

If the error you are getting is at the while loop then check if 'a' is nil before going into the while loop?
 

Haddock

New Member
Jul 29, 2019
5
0
0
Try this for that line:
Code:
while r<tostring(a) do
I'm afraid this still doesn't work :( As I still get this error message:
k8Sj5IR.png


Peppe - 'a' is definitely nil - what you are looking at there is the entire program so what has been defined there is everything.
 

Peppe

New Member
Jul 29, 2019
836
0
1
First issue i see then is there is no read() function, so x and y will always be nil.
print("Length (1st and 3rd side)")
x = read() <--- if read is not a function it will be null or nil LUA speak
print("Width (2nd and 4th side)")
y = read() <--- same as x

So later on your program you call:
runTime(x) <-- which is really sending runtime(nil)

Now in runtime:
function runTime(a)
r = 1
while r<a do
breakPlaceMove()
r = r + 1
end
end

First time it gets to the while loop it will get to:
while 1 < nil do

Comparing 1 to nil makes it sad.

You could fix in a few ways:
Make sure you always pass in a number.
Check for nil before making the comparison
Check for number before doing a numeric comparison -- on this you might have to check if it is nil before checking if it is numeric, depends on the language and i'm not super familiar with lua.

Something like:
function runTime(a)
r = 1
if a not nil then
while r<a do
breakPlaceMove()
r = r + 1
end
end
end

Syntax may not be exactly right. LUA is not my native language :p
 

Haddock

New Member
Jul 29, 2019
5
0
0
Yup thanks a lot peppe :D
I simply defined at the beginning of runTime(a) that
Code:
a = a + 0
That seemed to assure the computer that it was in fact dealing with a number not a string.
Thanks again man!
 

Poppycocks

New Member
Jul 29, 2019
1,914
0
0
Yup thanks a lot peppe :D
I simply defined at the beginning of runTime(a) that
Code:
a = a + 0
That seemed to assure the computer that it was in fact dealing with a number not a string.
Thanks again man!
or just a = 0