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

[LUA] Can't Reference a table value, please help

Started by Marini21, 14 October 2012 - 01:25 AM
Marini21 #1
Posted 14 October 2012 - 03:25 AM
In the following file, i can't reference any table value. says: Attemp to index a nil value ? at line 52. Help would be really appreciated.





function reFuel()
  if turtle.getFuelLevel()<50 then
	    print("Refueling...")
	    turtle.turnRight()
	    turtle.turnRight()
	    turtle.select(1)
	    turtle.suck()
	    turtle.drop(turtle.getItemCount()-2)
	    turtle.refuel(2)
	    turtle.turnLeft()
	    turtle.turnLeft()
	    turtle.select(1)
  end
end
function moveToChest(:)/>/>
  for i=1,b do
	    turtle.forward()
  end
  turtle.turnRight()
end
function goBackHome(:)/>/>
  turtle.turnLeft()
  for j=1,b do
	    turtle.back()
  end
  turtle.turnRight()
  turtle.drop()
  turtle.turnLeft()
end
function readName()
  rednet.open("right")
  while true do
	    turtle.select(1)
	    shell.run("clear")
	    print("Waiting for Server Instructions")
	    id,message = rednet.receive()                    
	    print(message)
	    if message == recipe[name] then 
		  rednet.send(id,"Getting Required Items")
		  craftRecipe(recipe[name])
		  rednet.send(id,"Crafting Complete")
	    else
		  rednet.send(id,"I Don't Know That Recipe")
		  sleep(2)
	    end
  end
end

function craftRecipe(recipe)
  moveToChest(recipe.chest)                      [b][size=5] -- HERE IS THE ERROR LINE[/size][/b]
  turtle.select(1)
  for k,v in pairs(recipe.map) do
	    turtle.select(k)
	    if v then 
		  turtle.suck()
		  turtle.drop(turtle.getItemCount(k)-1)						
	    end
  end
  turtle.craft()
  goBackHome(recipe.chest)                
end
-- End of Basic Functions
-- My crafting system is based on tables
recipe={
  ["stick"] = {
	    count=2,                                             [b][size=5]-- THESE ARE THE REFERENCED TABLES[/size][/b]
	    chest=3,
	    map={
		  [1]=false, [2]=false, [3]=false,
		  [5]=false, [6]=true, [7]=false,
		  [9]=false, [10]=true, [11]=false
	    }
  },
  ["furnace"] = {
	    count=8,
	    chest=4,
	    map={
		  [1]=true, [2]=true, [3]=true,
		  [5]=true, [6]=false, [7]=true,
		  [9]=true, [10]=true, [11]=true
	    }
  }
 
}
-- End of Recipes List
reFuel()
readName()
remiX #2
Posted 14 October 2012 - 04:22 AM

function moveToChest(:)/>/>
  for i=1,b do
            turtle.forward()
  end
  turtle.turnRight()
end


In the function, your argument is B but in the function you use a lower case b. That might be the prob,
PixelToast #3
Posted 14 October 2012 - 05:53 AM
another problem on line 38:
needs to be:

if recipe[message] then
that might work to fix your error
remiX #4
Posted 14 October 2012 - 11:44 AM
Also in your goBackHome function you use both lower and upper case b.
Marini21 #5
Posted 15 October 2012 - 12:45 AM
another problem on line 38:
needs to be:

if recipe[message] then
that might work to fix your error

i changed part of my code using the "for k,v in pairs" and it worked much better.
those upper case and lower case B, where just html messing up the post… in my code they are all lowercase, that wasn't the problem.. it's just that im unable to reference the table values