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

Error: attempt to index? (a nil value) for turtle.forward()

Started by Travio, 19 February 2014 - 04:07 PM
Travio #1
Posted 19 February 2014 - 05:07 PM
I have made a program that just moves a turtle back and forward between two coordinates when activated from a wireless computer.

I receive error: roof.lua:24: attempt to index ? (a nil value)

code below:


local locID = tonumber(os.getComputerLabel())
local oPos = vector.new(locID-14,63,182)
local cPos = vector.new(locID-14,63,215)
rednet.open("right")
repeat
local event,p1,p2,p3 = os.pullEvent()
if event=="rednet_message" and p1==13 and p2=="activate" then rednet.send(13,"Activating")
  local curPos = vector.new(gps.locate())
  local compO = curPos - cPos
  local compC = curPos - oPos
 
  if compC.x == 0 and compC.y == 0 and compC.z ==0 then
   rednet.send(13,"Turtle "..os.getComputerLabel().."opening")
   for x = 0,32,1 do
    turtle.back()
    turtle.place(1)
   end
  elseif compO.x == 0 and compO.y == 0 and compO.z ==0 then
   rednet.send(13,"Turtle "..os.getComputerLabel().."closing")
   for x = 0,32,1 do
    turtle.dig()
    turle.forward()
   end
  else
   print("Something went wrong here")
  end
 
elseif p2=="stop" then
   print("Stop command received from "..p1)
   rednet.send(13,"Turtle "..os.getComputerLabel().."stopping")
   os.queueEvent("char","x")
   else
    rednet.send(13,"Command not understood please try again")
  end
until event=="char" and p1=="x"
rednet.close("right")

Why does it fail with a simple command of turtle.forward() it worked when I was doing it with a single turtle but when I used two it didnt. However it successfully ran the opening for loop. just wouldnt run the closing part.

Any help appreciated. Thanks
Bomb Bloke #2
Posted 19 February 2014 - 07:17 PM
The first line attempts to store a numeric representation of the turtle's label. This will only work if the turtle's label can be represented as a number - if not, "locID" will be set to nil. It may be you wanted to use "os.getComputerID()" instead of "os.getComputerLabel()" there.

"turtle.place()" doesn't take parameters (unless you're trying to write a sign with it). It attempts to place one block from the currently selected inventory slot.

Must admit I've not experimented with vectors in Lua. Maybe test that the code that does the GPS location then subtracts from the resulting vector works on its own. I assume you've got sufficient GPS servers configured within range?

Edit: Oh, and you've spelt "turtle" incorrectly on line 22. Missing a "t" there.
Edited on 19 February 2014 - 06:18 PM
Goof #3
Posted 19 February 2014 - 07:17 PM
you missed a l in turtle…

Edit: Ninja'ed…

replace

for x = 0,32,1 do
	turtle.dig()
	turle.forward()
   end
with

for x = 0,32,1 do
	turtle.dig()
	turlle.forward()
   end
Edited on 19 February 2014 - 06:18 PM
Bomb Bloke #4
Posted 19 February 2014 - 07:20 PM
you missed a l in turtle…

Edit: Ninja'ed…
Heh, that's my line - you slipped in just before my edit, even if you got the character wrong. ;)/>
Travio #5
Posted 20 February 2014 - 08:07 AM
Goddamit :P/> thanks guys

Lua god, the vector stuff works, it gives me 0,0,0 and completes the first for loop, it just didnt work on the 2nd loop because of the missing L. (feeling retarded!) will fix it when I'm home from work.

Many thanks