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

craft :123: attempt to index? (a function value) --Please help!

Started by milkywave1, 25 November 2012 - 01:53 AM
milkywave1 #1
Posted 25 November 2012 - 02:53 AM
When I run my program with 'rednet' off, it's fine. However, when i run the program with 'rednet' on, it terminates and gives me this error: craft :123: attempt to index? (a function value) –Please help! craft is my program name :D/>/>
This is the code:

local fl = turtle.getFuelLevel()
local rn = false
--Functions
function forward(num)
  for a = 1,num do
  turtle.forward()
end
end
function shiftLeft()
  turtle.turnLeft()
  forward(1)
  turtle.turnRight()
end
function shiftRight()
  turtle.turnRight()
  forward(1)
  turtle.turnLeft()
end
function fuel()
  textutils.slowPrint("Current Fuel level: "..fl)
  if fl == 0 then
  textutils.slowPrint("Please add coal to slot 1")
end
end
function rprompt()
  write("Enable rednet? y or n :")
  check = io.read()

  if check == "y" then
  rn = true
  else if check == "n" then
  rn = false
  end
end
end
function prompt()
  write("How many pickaxes would you like to craft?:")
  input = read()

  write("What material would you like it to be made of? - Diamond/Gold/Iron :")
  input2 = read()

  print("Crafting "..input.." "..input2.." pickaxes")
end
function sticks()
  turtle.refuel()
  forward(3)
  shiftLeft()
  forward(1)
  turtle.suckUp()
  turtle.dropUp(turtle.getItemCount(1) - 2 * input)
  turtle.transferTo(6,turtle.getItemCount(1)/2)
  turtle.transferTo(10,turtle.getItemCount(1))
end
function materials()
  turtle.back()
  turtle.turnRight()
  forward(1)
  if input2 == "diamond" then
  forward(1)
  else if input2 == "gold" then
  forward(3)
  else if input2 == "iron" then
  forward(5)
  end
end
end
  turtle.turnLeft()
  forward(1)
  turtle.suckUp()
  turtle.dropUp(turtle.getItemCount(1) - 3 * input)
  turtle.transferTo(2, turtle.getItemCount(1) - 2 * input)
  turtle.transferTo(3, turtle.getItemCount(1) - turtle.getItemCount(1) / 2)
end
function craft()
  turtle.back()
  turtle.turnLeft()
  if input2 == "diamond" then
  forward(1)
  else if input2 == "gold" then
  forward(3)
  else if input2 == "iron" then
  forward(5)
  end
end
end
  turtle.turnRight()
  turtle.craft()
  turtle.back()
  turtle.back()
  turtle.back()
  turtle.turnRight()
  turtle.select(4)
  turtle.drop()
  turtle.turnLeft()
  turtle.select(1)
  if input ~= 1 then
  input = input -1
  for m = 1,input do
  forward(3)
  turtle.craft()
  turtle.back()
  turtle.back()
  turtle.back()
  turtle.turnRight()
  turtle.select(4)
  turtle.drop()
  turtle.turnLeft()
  end
end
end
function rednet()
  if rn == true then
  rednet.open("right")
  id,message = rednet.receive()
  term.clear()
  if message == "diamond" then
  input2 = "diamond"
  input = 1
  end
end
end

function main()
fuel()
rprompt()
rednet()
prompt()
sticks()
materials()
craft()
end
main()
turtle.select(1)
os.reboot()

Help will be much appreciated!
OmegaVest #2
Posted 25 November 2012 - 03:04 AM
rednet. You called one of your functions rednet, and you cannot do that, because it makes the computer think you are trying to overwrite the rednet api. Thus, when you call rednet.open() in the rednet function, it looks for a value within the rednet function named open(), not the api.
milkywave1 #3
Posted 25 November 2012 - 03:12 AM
rednet. You called one of your functions rednet, and you cannot do that, because it makes the computer think you are trying to overwrite the rednet api. Thus, when you call rednet.open() in the rednet function, it looks for a value within the rednet function named open(), not the api.
WOW! Thx! This works perfectly now :D/>/> Also, thx for the fast reply