Jump to content




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


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

#1 Travio

  • New Members
  • 2 posts

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

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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 by Bomb Bloke, 19 February 2014 - 07:18 PM.


#3 Goof

  • Members
  • 751 posts

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 by Mikk809h, 19 February 2014 - 07:18 PM.


#4 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 19 February 2014 - 07:20 PM

View PostMikk809h, on 19 February 2014 - 07:17 PM, said:

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. ;)

#5 Travio

  • New Members
  • 2 posts

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users