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

Problem with a function

Started by saschaepe, 05 January 2013 - 09:49 AM
saschaepe #1
Posted 05 January 2013 - 10:49 AM
i wantet to build a litte funtion for my turtle, but this code:


function GeheZu (zielx , zielz)
print(zielx)
print(zielz)
print("gehe zu")
while x < zielx and x < 18 do
  turtle.forward()
  x = x + 1
  sleep(0.5)
end
while x > zielx and x > 1 do
  turtle.back()
  x = x - 1
  sleep(0.5)
end
while z < zielz and z <3 do
  turtle.up()
  z = z + 1
  sleep(0.5)
end
while z > zielz and z > 1 do
  turtle.down()
  z = z - 1
  sleep(0.5)
end

print("fertig")
end

produce this error:


test:20: attemt to compare __lt on nil and number

hope someone can help me
Viproz #2
Posted 05 January 2013 - 11:00 AM
this is just a function so where is the line 20 of your code ?
GravityScore #3
Posted 05 January 2013 - 11:09 AM
Since this is a single function and we can't tell which line is line 20 of your code, I'm going to take a guess and say that either the variables x or z doesn't exist or hasn't been initialized before you call this function.

It would be of great help if you posted your full code.
Loki #4
Posted 05 January 2013 - 11:17 AM
1. Indent your code.
2. Show us line 20.
3. You probably havnt set any variables to be anything.
saschaepe #5
Posted 05 January 2013 - 11:20 AM
sry :rolleyes:/>
here the complete code



x, z = 1
iForm = 0
iEffekt = 0
iFarbe = 0
iStart = 0
function GeheZu (zielx , zielz)
print(zielx)
print(zielz)
print("gehe zu")
while x < zielx and x < 18 do
  turtle.forward()
  x = x + 1
  sleep(0.5)
end
while x > zielx and x > 1 do
  turtle.back()
  x = x - 1
  sleep(0.5)
end
while z < zielz and z <3 do
  turtle.up()
  z = z + 1
  sleep(0.5)
end
while z > zielz and z > 1 do
  turtle.down()
  z = z - 1
  sleep(0.5)
end

print("fertig")
end
-- Auswahlmenü --
shell.run("clear")
print("Waehle eine Form:")
print("1 - Kugel - klein")
print("2 - Kugel - gross")
print("3 - zerstreut")
print("4 - Stern")
print("5 - Creeper")
print("")
write("Gebe eine Zahl (1-5) ein: ")
while iForm == 0 do
form1 = io.read()
form = (form1 + 0)
if form >= 1 and form <= 5 then
  iForm = 1
else
  print("Falsche Eingabe! Bitte eine Zahl 1-5 eingeben!")
end
end
shell.run("clear")
print("Waehle einen Effekt:")
print("1 - keiner")
print("2 - Glitzern")
print("3 - Schweife")
print("")
write("Gebe eine Zahl (1-3) ein: ")
while iEffekt == 0 do
effekt1 = io.read()
effekt = (effekt1 + 0)
if effekt >= 1 and effekt <= 3 then
  iEffekt = 1
else
  print("Falsche Eingabe! Bitte eine Zahl 1-3 eingeben!")
end
end
shell.run("clear")
print("Waehle eine Farbe:")
print("1  - Weiss	   9  - Hellgrau")
print("2  - Orange	  10 - Tuerkis")
print("3  - Magenta	 11 - Violett")
print("4  - Hellblau	12 - Blau")
print("5  - Gelb		13 - Braun")
print("6  - Hellgruen   14 - Gruen")
print("7  - Rosa		15 - Rot")
print("8  - Grau		16 - Schwarz")
print("")
write("Gebe eine Zahl (1-16) ein: ")
while iFarbe == 0 do
farbe1 = io.read()
farbe = (farbe1 + 0)
if farbe >= 1 and farbe <= 16 then
  iFarbe = 1
else
  print("Falsche Eingabe! Bitte eine Zahl 1-16 eingeben!")
end
end
-- Startbefehl --
-- Schwarzpulver aufnehmen --
GeheZu(5,1)
turtle.select(1)
turtle.turnRight()
turtle.suck()
turtle.turnLeft()
GravityScore #6
Posted 05 January 2013 - 11:55 AM
Ah there is the problem. On the first line you do x, z = 1. This sets x to 1, but z is not set to anything, meaning it becomes nil. Try this instead: x, z = 1, 1.
saschaepe #7
Posted 07 January 2013 - 07:41 AM
that was the problem!

thank you :)/>