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

[Lua][Error]Attempt to index ?(a nil value)

Started by gatekeeper258, 08 May 2013 - 08:49 AM
gatekeeper258 #1
Posted 08 May 2013 - 10:49 AM
Title: [Lua][Error]Attempt to index ?(a nil value)

I am not a Smart person, just a Army Grunt trying to have fun in TurtleOS 1.5 for Minecraft Forge (FTB) Direwolf20 Pack 1.4.7. I wrote a program well, I'll just post it as I know you guys can read and understand in seconds what took me two weeks to put together and am having a strange issue I cannot diagnose.


if turtle.select(1) then
print("Selected Slot!")
else
  print("No slot selected!")
  end
while turtle.detect() do
  --other code
	if turtle.detectUp() then
	  print("I am Discharger!")
	  print("der discharge!")
	  getItem()
	  turtle.dropUp()
	  sleep(10)
	  turtle.suckUp()
	  turtle.drop()
	else
	  print("I am Charger!")
	  sleep(2)
	  print("charge!")
	  getItem()
	  turtle.dropDown()
	  sleep(15)
	  turtle.suckDown()
	  turtle.drop()
	end
  os.queueEvent("event_marker")
  local breakOut = false
  while true do
	e = os.pullEvent()
	if e == "key" then --add any other events that should break the loop if necessary
	  breakOut = true
	elseif e == "event_marker" then
	  break
	end
  end
  if breakOut then break end
end

function getItem()
turtle.turnLeft()
   if turtle.suck() then
	 print("Acquired item!")
	else
	 print("Did not get item!")
	 sleep(5)
   end
turtle.turnRight()
end

http://pastebin.com/FqiCqSwJ
So the ggetItem() functions is returning nil. but if ran standalong (typing manually in the lua interp it works great) but in a function causes problems. Any ideas?
Dlcruz129 #2
Posted 08 May 2013 - 11:15 AM
You need to define a function before you run it. Move the function getItem()…end to the top of the program.