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

[LUA]Just can't figure out what the problem is

Started by milkywave1, 03 December 2012 - 08:36 PM
milkywave1 #1
Posted 03 December 2012 - 09:36 PM
I just can't figure out why this is not working, i tried everything but it only turns Right and turns left

EDIT: Also, if you know how to make my code shorter, tell me please :D/>

the turtle:
Spoiler

--Vars
local a = 0
local sIndex = 1
local material = 1
local util = 1
local item = 1
local state = 1
--FUNCTIONS
local function forward(num)
	for i = 1,num do
		turtle.forward()
	end
end
local function shiftLeft(num)
	turtle.turnLeft()
	for i = 1,num do
		turtle.forward()
	end
	turtle.turnRight()
end
local function shiftRight(num)
	turtle.turnRight()
	for i = 1,num do
		turtle.forward()
	end
	turtle.turnLeft()
end
local function VertMenu(tableofItems)
	local selection = "";
	while (selection == "") do
		term.clear();
		local xMax, yMax = term.getSize();
		local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
		y = y - #tableofItems;
		if y<1 then y = 1 end;
		if #tableofItems > yMax then
			print "ERROR: List does not fit on screen, reduce number of items in table";
			return "";
		end
		for i = 1, #tableofItems do
			if string.len(tableofItems[i]) >= xMax then
				term.clear();
				term.setCursorPos(1,1);
				print (("ERROR: List item # "..i).." is too long please reduce");
				return "";
			end
			local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
			if i == sIndex then x = x-1 end;
			term.setCursorPos(x,y);
			if i == sIndex then term.write("[") end;
			term.write(tableofItems[i]);
			if i == sIndex then term.write("]") end;
			y = y+1;
		end
		local r,s = os.pullEvent("key");
		if s == 208 then sIndex = sIndex +1 end;
		if s == 200 then sIndex = sIndex -1 end;
		if sIndex > #tableofItems then sIndex = 1 end;
		if sIndex < 1 then sIndex = #tableofItems end;
		if s == 28 then
			term.clear();
			term.setCursorPos(1,1);
			return tableofItems[sIndex]
		end
	end
end
local function materialMenu(tableofItems)
	local selection = "";
	while (selection == "") do
		term.clear();
		local xMax, yMax = term.getSize();
		local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
		y = y - #tableofItems;
		if y<1 then y = 1 end;
		if #tableofItems > yMax then
			print "ERROR: List does not fit on screen, reduce number of items in table";
			return "";
		end
		for i = 1, #tableofItems do
			if string.len(tableofItems[i]) >= xMax then
				term.clear();
				term.setCursorPos(1,1);
				print (("ERROR: List item # "..i).." is too long please reduce");
				return "";
			end
			local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
			if i == material then x = x-1 end;
			term.setCursorPos(x,y);
			if i == material then term.write("[") end;
			term.write(tableofItems[i]);
			if i == material then term.write("]") end;
			y = y+1;
		end
		local r,s = os.pullEvent("key");
		if s == 208 then material = material +1 end;
		if s == 200 then material = material -1 end;
		if material > #tableofItems then material = 1 end;
		if material < 1 then material = #tableofItems end;
		if s == 28 then
			term.clear();
			term.setCursorPos(1,1);
			return tableofItems[material]
		end
	end
end
local function ItemMenu(tableofItems)
	local selection = "";
	while (selection == "") do
		term.clear();
		local xMax, yMax = term.getSize();
		local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
		y = y - #tableofItems;
		if y<1 then y = 1 end;
		if #tableofItems > yMax then
			print "ERROR: List does not fit on screen, reduce number of items in table";
			return "";
		end
		for i = 1, #tableofItems do
			if string.len(tableofItems[i]) >= xMax then
				term.clear();
				term.setCursorPos(1,1);
				print (("ERROR: List item # "..i).." is too long please reduce");
				return "";
			end
			local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
			if i == item then x = x-1 end;
			term.setCursorPos(x,y);
			if i == item then term.write("[") end;
			term.write(tableofItems[i]);
			if i == item then term.write("]") end;
			y = y+1;
		end
		local r,s = os.pullEvent("key");
		if s == 208 then item = item +1 end;
		if s == 200 then item = item -1 end;
		if item > #tableofItems then item = 1 end;
		if item < 1 then item = #tableofItems end;
		if s == 28 then
			term.clear();
			term.setCursorPos(1,1);
			return tableofItems[item]
		end
	end
end
local function UtilityMenu(tableofItems)
	local selection = "";
	while (selection == "") do
		term.clear();
		local xMax, yMax = term.getSize();
		local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
		y = y - #tableofItems;
		if y<1 then y = 1 end;
		if #tableofItems > yMax then
			print "ERROR: List does not fit on screen, reduce number of items in table";
			return "";
		end
		for i = 1, #tableofItems do
			if string.len(tableofItems[i]) >= xMax then
				term.clear();
				term.setCursorPos(1,1);
				print (("ERROR: List item # "..i).." is too long please reduce");
				return "";
			end
			local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
			if i == util then x = x-1 end;
			term.setCursorPos(x,y);
			if i == util then term.write("[") end;
			term.write(tableofItems[i]);
			if i == util then term.write("]") end;
			y = y+1;
		end
		local r,s = os.pullEvent("key");
		if s == 208 then util = util +1 end;
		if s == 200 then util = util -1 end;
		if util > #tableofItems then util = 1 end;
		if util < 1 then util = #tableofItems end;
		if s == 28 then
			term.clear();
			term.setCursorPos(1,1);
			return tableofItems[item]
		end
	end
end
local function category()
	ItemMenu({"Tools", "Utility","Rednet"})
	if item == 1 then
		VertMenu({"Sword","Shovel","Pickaxe","Axe","Hoe"})
		materialMenu({"Diamond", "Gold", "Iron", "Stone", "Wood"})
	elseif item == 2 then
		UtilityMenu({"Furnace","Crafting Table", "Chest"})
	end
end
local function fuel()
	fuel = turtle.getFuelLevel()
	print("Current fuel level: "..fuel)
	if fuel == 0 then
		print("No fuel! Put some coal in slot 16!")
	end
end
local function prompt()
  if (item == 1) and (item == 2) then
	write("Quantity: ")
	number = read()
	number = tonumber(number)
  end
	turtle.select(16)
	turtle.refuel()
	turtle.select(1)
	if item == 1 then
		tools()
	elseif item == 2 then
		utility()
	end
end
local function tools()
	if sIndex == 1 then
		forward(1)
		turtle.turnLeft()
		forward(1)
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - number)
		turtle.transferTo(10, number)
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - number * 2)
		turtle.transferTo(6, number)
		turtle.transferTo(2, number)
	elseif sIndex == 2 then
		handle()
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - number)
		turtle.transferTo(2, number)
	elseif sIndex == 3 then
		handle()
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 3 * number)
		turtle.transferTo(2, number)
		turtle.transferTo(3, number)
	elseif sIndex == 4 then
		handle()
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 3 * number)
		turtle.transferTo(2, number)
		turtle.transferTo(3, number)
		turtle.transferTo(7, number)
	  elseif sIndex == 5 then
		handle()
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 2 * number)
		turtle.transferTo(2, number)
		turtle.transferTo(3, number)
	end
end
local function materialTools()
	if material == 1 then -- Diamond
		turtle.up()
		forward(1)
	elseif material == 2 then -- Gold
		shiftRight(3)
	elseif material == 3 then -- Iron
		shiftRight(3)
		turtle.up()
		forward(1)
	elseif material == 4 then -- Stone
		shiftRight(6)
	elseif material == 5 then -- Wood
		shiftRight(6)
		turtle.up()
		forward(1)
	end
end
local function utility()
	if util == 1 then
		forward(1)
		turtle.turnLeft()
		forward(1)
		shiftRight(6)
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 8 * number)
		for i = 1,11 do
			if (i ~= 4) and (i ~= 6) and (i ~= 8) then
				turtle.transferTo(i, number)
			end
		end
	elseif util == 2 then
		forward(1)
		turtle.turnLeft()
		forward(1)
		shiftRight(6)
		turtle.up()
		forward(1)
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 4 * number)
		for i = 1,6 do
			if (i ~=3) and (i ~=4) then
				turtle.transferTo(i, number)
			end
		end
	elseif util == 3 then
		forward(1)
		turtle.turnLeft()
		forward(1)
		shiftRight(6)
		turtle.up()
		forward(1)
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 8 * number)
		for i = 1,11 do
			if (i ~=4) and (i ~= 6) and (i ~= 8) then
				turtle.transferTo(i,number)
			end
		end
	end
end
local function handle()
	forward(1)
	turtle.turnLeft()
	forward(1)
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - 2 * number)
	turtle.transferTo(10, number)
	turtle.transferTo(6, number)
end
local function craft()
	for i = 1,number do
		turtle.craft()
		turtle.turnRight()
		turtle.drop()
		turtle.turnLeft()
	end
end
local function reset()
	if item == 1 then -- Tools
		if material == 1 then
			turtle.back()
			turtle.down()
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 2 then
			shiftLeft(3)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 3 then
			turtle.back()
			turtle.down()
			shiftLeft(3)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 4 then
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 5 then
			turtle.back()
			turtle.down()
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		end --if
	elseif item == 2 then
		if util == 1 then
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif (util == 2) or (util == 3) then
			turtle.back()
			turtle.down()
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		end
	end
end
local function redn()
  number = 1
  if item == 3 then
	rednet.open("right")
	id,message = rednet.receive()
	print("Waiting for command...")
	if message == "sword" then
	  sIndex = 1
	elseif message == "shovel" then
	  sIndex = 2
	elseif message == "pickaxe" then
	  sIndex = 3
	elseif message == "axe" then
	  sIndex = 4
	elseif message == "hoe" then
	  sIndex = 5
	end
	sleep(2)
	if message == "diamond" then
	  material = 1
	elseif message == "gold" then
	  material = 2
	elseif message == "iron" then
	  material = 3
	elseif message == "stone" then
	  material = 4
	elseif message == "wood" then
	  material = 5
	end
	if message == "furnace" then
	  util = 1
	elseif message == "craftingtable" then
	  util = 2
	elseif message == "chest" then
	  util = 3
	end
  end
end
local function main()
	category()
	redn()
	fuel()
	prompt()
	reset()
	craft()
	term.clear()
	term.setCursorPos(1,1)
end
main()

the computer:
Spoiler

local mode = 1
local craftmode = 1
local util = 1
local tool = 1
local material = 1
local function mainMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == mode then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == mode then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == mode then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then mode = mode +1 end;
	if s == 200 then mode = mode -1 end;
	if mode > #tableofItems then mode = 1 end;
	if mode < 1 then mode = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[mode]
	end
  end
end
local function craftMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == craftmode then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == craftmode then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == craftmode then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then craftmode = craftmode +1 end;
	if s == 200 then craftmode = craftmode -1 end;
	if craftmode > #tableofItems then craftmode = 1 end;
	if craftmode < 1 then craftmode = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[craftmode]
	end
  end
end
local function utilMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == util then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == util then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == util then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then util = util +1 end;
	if s == 200 then util = util -1 end;
	if util > #tableofItems then util = 1 end;
	if util < 1 then util = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[util]
	end
  end
end
local function toolMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == tool then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == tool then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == tool then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then tool = tool +1 end;
	if s == 200 then tool = tool -1 end;
	if tool > #tableofItems then tool = 1 end;
	if tool < 1 then tool = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[tool]
	end
  end
end
local function materialMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == material then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == material then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == material then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then material = material +1 end;
	if s == 200 then material = material -1 end;
	if material > #tableofItems then material = 1 end;
	if material < 1 then material = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[material]
	end
  end
end
function menus()
  rednet.open("top")
  mainMenu({"Craft items"})
  craftMenu({"Tools","Utility"})
  if craftmode == 1 then
	toolMenu({"Sword","Shovel","Pickaxe","Axe","Hoe"})
	materialMenu({"Diamond","Gold","Iron","Stone","Wood"})
  elseif craftmode == 2 then
	utilMenu({"Furnace","Crafting table","Chest"})
  end
end
function redn()
  if craftmode == 1 then
	if tool == 1 then
	  rednet.send(21, "sword")
	elseif tool == 2 then
	  rednet.send(21, "shovel")
	elseif tool == 3 then
	  rednet.send(21, "pickaxe")
	elseif tool == 4 then
	  rednet.send(21, "axe")
	elseif tool == 5 then
	  rednet.send(21, "hoe")
	end
	sleep(2)
	if material == 1 then
	  rednet.send(21,"diamond")
	elseif material == 2 then
	  rednet.send(21, "gold")
	elseif material == 3 then
	  rednet.send(21,"iron")
	elseif material == 4 then
	  rednet.send(21, "stone")
	elseif material == 5 then
	  rednet.send(21, "wood")
	end
  elseif craftmode == 2 then
	if util == 1 then
	  rednet.send(21,"furnace")
	elseif util == 2 then
	  rednet.send(21, "craftingtable")
	elseif util == 3 then
	  rednet.send(21, "chest")
	end
  end
end
function main()
  menus()
  redn()
end
main()
Zoinky #2
Posted 03 December 2012 - 10:11 PM
Could you put your code in spoilers or use pastebin? It's a little large.
milkywave1 #3
Posted 04 December 2012 - 12:50 AM
Could you put your code in spoilers or use pastebin? It's a little large.

Done.
Doyle3694 #4
Posted 04 December 2012 - 02:47 AM
Havn't looked at the code, but is the turtle fueled?
milkywave1 #5
Posted 04 December 2012 - 04:01 AM
Havn't looked at the code, but is the turtle fueled?

Yea, the last time I checked it was quite high
ChunLing #6
Posted 04 December 2012 - 08:38 AM
You don't have a program loop. It just runs once and it's done.
milkywave1 #7
Posted 04 December 2012 - 01:21 PM
You don't have a program loop. It just runs once and it's done.
The problem is with the rednet. It works without rednet but once I activate rednet the problem happens
ChunLing #8
Posted 04 December 2012 - 03:39 PM
Okay, looking at your rnet function that doesn't do what you seem to think it does, but the only thing it's really doing of concern is that it overrides the value of number to be 1, which prompt should reset later, but doesn't because it only works if 1==2 (which cannot happen, in this universe at least).

The real horror is fuel, you call the function and it promptly overwrites itself with an integer. If you did have a program loop, the second time you tried to call fuel it would produce an error for sure.

When you say "It works without rednet"…what exactly do you mean?
milkywave1 #9
Posted 04 December 2012 - 04:53 PM
Okay, looking at your rnet function that doesn't do what you seem to think it does, but the only thing it's really doing of concern is that it overrides the value of number to be 1, which prompt should reset later, but doesn't because it only works if 1==2 (which cannot happen, in this universe at least).

The real horror is fuel, you call the function and it promptly overwrites itself with an integer. If you did have a program loop, the second time you tried to call fuel it would produce an error for sure.

When you say "It works without rednet"…what exactly do you mean?
I meant that the rednet wasnt on ^_^/>
milkywave1 #10
Posted 04 December 2012 - 05:02 PM
Ok, so I THINK I got it, but it gives me an error: craft:258: attempt to call nil

Heres the current code:
Spoiler

--Vars
local a = 0
local sIndex = 1
local material = 1
local util = 1
local item = 1
local state = 1
local a = 0
--FUNCTIONS
local function forward(num)
	for i = 1,num do
		turtle.forward()
	end
end
local function shiftLeft(num)
	turtle.turnLeft()
	for i = 1,num do
		turtle.forward()
	end
	turtle.turnRight()
end
local function shiftRight(num)
	turtle.turnRight()
	for i = 1,num do
		turtle.forward()
	end
	turtle.turnLeft()
end
local function VertMenu(tableofItems)
	local selection = "";
	while (selection == "") do
		term.clear();
		local xMax, yMax = term.getSize();
		local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
		y = y - #tableofItems;
		if y<1 then y = 1 end;
		if #tableofItems > yMax then
			print "ERROR: List does not fit on screen, reduce number of items in table";
			return "";
		end
		for i = 1, #tableofItems do
			if string.len(tableofItems[i]) >= xMax then
				term.clear();
				term.setCursorPos(1,1);
				print (("ERROR: List item # "..i).." is too long please reduce");
				return "";
			end
			local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
			if i == sIndex then x = x-1 end;
			term.setCursorPos(x,y);
			if i == sIndex then term.write("[") end;
			term.write(tableofItems[i]);
			if i == sIndex then term.write("]") end;
			y = y+1;
		end
		local r,s = os.pullEvent("key");
		if s == 208 then sIndex = sIndex +1 end;
		if s == 200 then sIndex = sIndex -1 end;
		if sIndex > #tableofItems then sIndex = 1 end;
		if sIndex < 1 then sIndex = #tableofItems end;
		if s == 28 then
			term.clear();
			term.setCursorPos(1,1);
			return tableofItems[sIndex]
		end
	end
end
local function materialMenu(tableofItems)
	local selection = "";
	while (selection == "") do
		term.clear();
		local xMax, yMax = term.getSize();
		local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
		y = y - #tableofItems;
		if y<1 then y = 1 end;
		if #tableofItems > yMax then
			print "ERROR: List does not fit on screen, reduce number of items in table";
			return "";
		end
		for i = 1, #tableofItems do
			if string.len(tableofItems[i]) >= xMax then
				term.clear();
				term.setCursorPos(1,1);
				print (("ERROR: List item # "..i).." is too long please reduce");
				return "";
			end
			local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
			if i == material then x = x-1 end;
			term.setCursorPos(x,y);
			if i == material then term.write("[") end;
			term.write(tableofItems[i]);
			if i == material then term.write("]") end;
			y = y+1;
		end
		local r,s = os.pullEvent("key");
		if s == 208 then material = material +1 end;
		if s == 200 then material = material -1 end;
		if material > #tableofItems then material = 1 end;
		if material < 1 then material = #tableofItems end;
		if s == 28 then
			term.clear();
			term.setCursorPos(1,1);
			return tableofItems[material]
		end
	end
end
local function ItemMenu(tableofItems)
	local selection = "";
	while (selection == "") do
		term.clear();
		local xMax, yMax = term.getSize();
		local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
		y = y - #tableofItems;
		if y<1 then y = 1 end;
		if #tableofItems > yMax then
			print "ERROR: List does not fit on screen, reduce number of items in table";
			return "";
		end
		for i = 1, #tableofItems do
			if string.len(tableofItems[i]) >= xMax then
				term.clear();
				term.setCursorPos(1,1);
				print (("ERROR: List item # "..i).." is too long please reduce");
				return "";
			end
			local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
			if i == item then x = x-1 end;
			term.setCursorPos(x,y);
			if i == item then term.write("[") end;
			term.write(tableofItems[i]);
			if i == item then term.write("]") end;
			y = y+1;
		end
		local r,s = os.pullEvent("key");
		if s == 208 then item = item +1 end;
		if s == 200 then item = item -1 end;
		if item > #tableofItems then item = 1 end;
		if item < 1 then item = #tableofItems end;
		if s == 28 then
			term.clear();
			term.setCursorPos(1,1);
			return tableofItems[item]
		end
	end
end
local function UtilityMenu(tableofItems)
	local selection = "";
	while (selection == "") do
		term.clear();
		local xMax, yMax = term.getSize();
		local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
		y = y - #tableofItems;
		if y<1 then y = 1 end;
		if #tableofItems > yMax then
			print "ERROR: List does not fit on screen, reduce number of items in table";
			return "";
		end
		for i = 1, #tableofItems do
			if string.len(tableofItems[i]) >= xMax then
				term.clear();
				term.setCursorPos(1,1);
				print (("ERROR: List item # "..i).." is too long please reduce");
				return "";
			end
			local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
			if i == util then x = x-1 end;
			term.setCursorPos(x,y);
			if i == util then term.write("[") end;
			term.write(tableofItems[i]);
			if i == util then term.write("]") end;
			y = y+1;
		end
		local r,s = os.pullEvent("key");
		if s == 208 then util = util +1 end;
		if s == 200 then util = util -1 end;
		if util > #tableofItems then util = 1 end;
		if util < 1 then util = #tableofItems end;
		if s == 28 then
			term.clear();
			term.setCursorPos(1,1);
			return tableofItems[item]
		end
	end
end
local function category()
	ItemMenu({"Tools", "Utility","Rednet"})
	if item == 1 then
		VertMenu({"Sword","Shovel","Pickaxe","Axe","Hoe"})
		materialMenu({"Diamond", "Gold", "Iron", "Stone", "Wood"})
	elseif item == 2 then
		UtilityMenu({"Furnace","Crafting Table", "Chest"})
	end
end
local function fuel()
	fuel = turtle.getFuelLevel()
	print("Current fuel level: "..fuel)
	if fuel == 0 then
		print("No fuel! Put some coal in slot 16!")
	end
end
local function prompt()
  if a == 0 then
	write("Quantity: ")
	number = read()
	number = tonumber(number)
  end
	turtle.select(16)
	turtle.refuel()
	turtle.select(1)
	if item == 1 then
		tools()
	elseif item == 2 then
		utility()
	end
end
local function tools()
	if sIndex == 1 then
		forward(1)
		turtle.turnLeft()
		forward(1)
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - number)
		turtle.transferTo(10, number)
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - number * 2)
		turtle.transferTo(6, number)
		turtle.transferTo(2, number)
	elseif sIndex == 2 then
		handle()
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - number)
		turtle.transferTo(2, number)
	elseif sIndex == 3 then
		handle()
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 3 * number)
		turtle.transferTo(2, number)
		turtle.transferTo(3, number)
	elseif sIndex == 4 then
		handle()
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 3 * number)
		turtle.transferTo(2, number)
		turtle.transferTo(3, number)
		turtle.transferTo(7, number)
	  elseif sIndex == 5 then
		handle()
		materialTools()
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 2 * number)
		turtle.transferTo(2, number)
		turtle.transferTo(3, number)
	end
end
local function materialTools()
	if material == 1 then -- Diamond
		turtle.up()
		forward(1)
	elseif material == 2 then -- Gold
		shiftRight(3)
	elseif material == 3 then -- Iron
		shiftRight(3)
		turtle.up()
		forward(1)
	elseif material == 4 then -- Stone
		shiftRight(6)
	elseif material == 5 then -- Wood
		shiftRight(6)
		turtle.up()
		forward(1)
	end
end
local function utility()
	if util == 1 then
		forward(1)
		turtle.turnLeft()
		forward(1)
		shiftRight(6)
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 8 * number)
		for i = 1,11 do
			if (i ~= 4) and (i ~= 6) and (i ~= 8) then
				turtle.transferTo(i, number)
			end
		end
	elseif util == 2 then
		forward(1)
		turtle.turnLeft()
		forward(1)
		shiftRight(6)
		turtle.up()
		forward(1)
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 4 * number)
		for i = 1,6 do
			if (i ~=3) and (i ~=4) then
				turtle.transferTo(i, number)
			end
		end
	elseif util == 3 then
		forward(1)
		turtle.turnLeft()
		forward(1)
		shiftRight(6)
		turtle.up()
		forward(1)
		turtle.suck()
		turtle.drop(turtle.getItemCount(1) - 8 * number)
		for i = 1,11 do
			if (i ~=4) and (i ~= 6) and (i ~= 8) then
				turtle.transferTo(i,number)
			end
		end
	end
end
local function handle()
	forward(1)
	turtle.turnLeft()
	forward(1)
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - 2 * number)
	turtle.transferTo(10, number)
	turtle.transferTo(6, number)
end
local function craft()
	for i = 1,number do
		turtle.craft()
		turtle.turnRight()
		turtle.drop()
		turtle.turnLeft()
	end
end
local function reset()
	if item == 1 then -- Tools
		if material == 1 then
			turtle.back()
			turtle.down()
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 2 then
			shiftLeft(3)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 3 then
			turtle.back()
			turtle.down()
			shiftLeft(3)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 4 then
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 5 then
			turtle.back()
			turtle.down()
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		end --if
	elseif item == 2 then
		if util == 1 then
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif (util == 2) or (util == 3) then
			turtle.back()
			turtle.down()
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		end
	end
end
local function redn()
  number = 1
  if item == 3 then
	a = 1
	rednet.open("right")
	id,message = rednet.receive()
	print("Waiting for command...")
	if message == "sword" then
	  sIndex = 1
	  item = 1
	elseif message == "shovel" then
	  sIndex = 2
	  item = 1
	elseif message == "pickaxe" then
	  sIndex = 3
	  item = 1
	elseif message == "axe" then
	  sIndex = 4
	  item = 1
	elseif message == "hoe" then
	  sIndex = 5
	  item = 1
	end
	sleep(2)
	if message == "diamond" then
	  material = 1
	elseif message == "gold" then
	  material = 2
	elseif message == "iron" then
	  material = 3
	elseif message == "stone" then
	  material = 4
	elseif message == "wood" then
	  material = 5
	end
	if message == "furnace" then
	  util = 1
	  item = 2
	elseif message == "craftingtable" then
	  util = 2
	  item = 2
	elseif message == "chest" then
	  util = 3
	  item = 2
	end
  end
end
local function main()
	category()
	redn()
	fuel()
	prompt()
	reset()
	craft()
	term.clear()
	term.setCursorPos(1,1)
end
main()

And I think line 258 is the utility function called in the prompt function

EDIT: I think I screwed the code because it still gives me the attempt to call nil error when I dont on rednet
KaoS #11
Posted 04 December 2012 - 07:36 PM
you are creating the function materialTools after you are trying to use it so the program will not yet know what materialTools is which is why it thinks you are calling nil
milkywave1 #12
Posted 04 December 2012 - 10:55 PM
you are creating the function materialTools after you are trying to use it so the program will not yet know what materialTools is which is why it thinks you are calling nil
It works! Thx :D/>
milkywave1 #13
Posted 04 December 2012 - 11:54 PM
you are creating the function materialTools after you are trying to use it so the program will not yet know what materialTools is which is why it thinks you are calling nil
It works! Thx :D/>

Ok, now it gives me an error
turtle:
Spoiler

--Vars
local a = 0
local sIndex = 1
local material = 1
local util = 1
local item = 1
--FUNCTIONS
local function forward(num)
	for i = 1,num do
		turtle.forward()
	end
end
local function shiftLeft(num)
	turtle.turnLeft()
	for i = 1,num do
		turtle.forward()
	end
	turtle.turnRight()
end
local function shiftRight(num)
	turtle.turnRight()
	for i = 1,num do
		turtle.forward()
	end
	turtle.turnLeft()
end
local function VertMenu(tableofItems)
	local selection = "";
	while (selection == "") do
		term.clear();
		local xMax, yMax = term.getSize();
		local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
		y = y - #tableofItems;
		if y<1 then y = 1 end;
		if #tableofItems > yMax then
			print "ERROR: List does not fit on screen, reduce number of items in table";
			return "";
		end
		for i = 1, #tableofItems do
			if string.len(tableofItems[i]) >= xMax then
				term.clear();
				term.setCursorPos(1,1);
				print (("ERROR: List item # "..i).." is too long please reduce");
				return "";
			end
			local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
			if i == item then x = x-1 end;
			term.setCursorPos(x,y);
			if i == item then term.write("[") end;
			term.write(tableofItems[i]);
			if i == item then term.write("]") end;
			y = y+1;
		end
		local r,s = os.pullEvent("key");
		if s == 208 then item = item +1 end;
		if s == 200 then item = item -1 end;
		if item > #tableofItems then item = 1 end;
		if item < 1 then item = #tableofItems end;
		if s == 28 then
			term.clear();
			term.setCursorPos(1,1);
			return tableofItems[item]
		end
	end
end
local function handle()
  forward(1)
  turtle.turnLeft()
  forward(1)
  turtle.suck()
  turtle.drop(turtle.getItemCount(1) - 2 * number)
  turtle.transferTo(10, number)
  turtle.transferTo(6, number)
end
local function fuel()
	fuel = turtle.getFuelLevel()
	print("Current fuel level: "..fuel)
	if fuel == 0 then
		print("No fuel! Put some coal in slot 16!")
	end
end
local function materialTools()
  if material == 1 then -- Diamond
	turtle.up()
	forward(1)
  elseif material == 2 then -- Gold
	shiftRight(3)
  elseif material == 3 then -- Iron
	shiftRight(3)
	turtle.up()
	forward(1)
  elseif material == 4 then -- Stone
	shiftRight(6)
  elseif material == 5 then -- Wood
	shiftRight(6)
	turtle.up()
	forward(1)
  end
end
local function tools()
  if sIndex == 1 then
	forward(1)
	turtle.turnLeft()
	forward(1)
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - number)
	turtle.transferTo(10, number)
	materialTools()
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - number * 2)
	turtle.transferTo(6, number)
	turtle.transferTo(2, number)
  elseif sIndex == 2 then
	handle()
	materialTools()
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - number)
	turtle.transferTo(2, number)
  elseif sIndex == 3 then
	handle()
	materialTools()
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - 3 * number)
	turtle.transferTo(2, number)
	turtle.transferTo(3, number)
  elseif sIndex == 4 then
	handle()
	materialTools()
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - 3 * number)
	turtle.transferTo(2, number)
	turtle.transferTo(3, number)
	turtle.transferTo(7, number)
  elseif sIndex == 5 then
	handle()
	materialTools()
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - 2 * number)
	turtle.transferTo(2, number)
	turtle.transferTo(3, number)
  end
end
local function utility()
  if util == 1 then
	forward(1)
	turtle.turnLeft()
	forward(1)
	shiftRight(6)
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - 8 * number)
	for i = 1,11 do
	  if (i ~= 4) and (i ~= 6) and (i ~= 8) then
		turtle.transferTo(i, number)
	  end
	end
  elseif util == 2 then
	forward(1)
	turtle.turnLeft()
	forward(1)
	shiftRight(6)
	turtle.up()
	forward(1)
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - 4 * number)
	for i = 1,6 do
	  if (i ~=3) and (i ~=4) then
		turtle.transferTo(i, number)
	  end
	end
  elseif util == 3 then
	forward(1)
	turtle.turnLeft()
	forward(1)
	shiftRight(6)
	turtle.up()
	forward(1)
	turtle.suck()
	turtle.drop(turtle.getItemCount(1) - 8 * number)
	for i = 1,11 do
	  if (i ~=4) and (i ~= 6) and (i ~= 8) then
		turtle.transferTo(i,number)
	  end
	end
  end
end
local function prompt()
	turtle.select(16)
	turtle.refuel()
	turtle.select(1)
	if item == 1 then
		tools()
	elseif item == 2 then
		utility()
	end
end
local function craft()
	for i = 1,number do
		turtle.craft()
		turtle.turnRight()
		turtle.drop()
		turtle.turnLeft()
	end
end
local function reset()
	if item == 1 then -- Tools
		if material == 1 then
			turtle.back()
			turtle.down()
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 2 then
			shiftLeft(3)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 3 then
			turtle.back()
			turtle.down()
			shiftLeft(3)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 4 then
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif material == 5 then
			turtle.back()
			turtle.down()
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		end --if
	elseif item == 2 then
		if util == 1 then
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		elseif (util == 2) or (util == 3) then
			turtle.back()
			turtle.down()
			shiftLeft(6)
			turtle.back()
			turtle.turnRight()
			turtle.back()
		end
	end
end
local function redn()
  if item == 1 then
	rednet.open("right")
	print("Waiting for command...")
	id,message = rednet.receive()
	if message == "sword" then
	  sIndex = 1
	  item = 1
	elseif message == "shovel" then
	  sIndex = 2
	  item = 1
	elseif message == "pickaxe" then
	  sIndex = 3
	  item = 1
	elseif message == "axe" then
	  sIndex = 4
	  item = 1
	elseif message == "hoe" then
	  sIndex = 5
	  item = 1
	end
	sleep(2)
	if message == "diamond" then
	  material = 1
	elseif message == "gold" then
	  material = 2
	elseif message == "iron" then
	  material = 3
	elseif message == "stone" then
	  material = 4
	elseif message == "wood" then
	  material = 5
	end
	if message == "furnace" then
	  util = 1
	  item = 2
	elseif message == "craftingtable" then
	  util = 2
	  item = 2
	elseif message == "chest" then
	  util = 3
	  item = 2
	end
	sleep(2)
	number = tonumber(message)
  end
end
local function quit()
  if item == 2 then
	error()
  end
end
local function main()
	VertMenu({"Rednet","Back"})
	quit()
	redn()
	fuel()
	prompt()
	reset()
	craft()
	term.clear()
	term.setCursorPos(1,1)
end
main()
the computer
Spoiler

local mode = 1
local craftmode = 1
local util = 1
local tool = 1
local material = 1
local function mainMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == mode then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == mode then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == mode then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then mode = mode +1 end;
	if s == 200 then mode = mode -1 end;
	if mode > #tableofItems then mode = 1 end;
	if mode < 1 then mode = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[mode]
	end
  end
end
local function craftMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == craftmode then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == craftmode then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == craftmode then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then craftmode = craftmode +1 end;
	if s == 200 then craftmode = craftmode -1 end;
	if craftmode > #tableofItems then craftmode = 1 end;
	if craftmode < 1 then craftmode = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[craftmode]
	end
  end
end
local function utilMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == util then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == util then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == util then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then util = util +1 end;
	if s == 200 then util = util -1 end;
	if util > #tableofItems then util = 1 end;
	if util < 1 then util = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[util]
	end
  end
end
local function toolMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == tool then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == tool then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == tool then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then tool = tool +1 end;
	if s == 200 then tool = tool -1 end;
	if tool > #tableofItems then tool = 1 end;
	if tool < 1 then tool = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[tool]
	end
  end
end
local function materialMenu(tableofItems)
  local selection = "";
  while (selection == "") do
	term.clear();
	local xMax, yMax = term.getSize();
	local y = math.floor(yMax/2) + math.floor(#tableofItems/2);
	y = y - #tableofItems;
	if y<1 then y = 1 end;
	if #tableofItems > yMax then
	  print "ERROR: List does not fit on screen, reduce number of items in table";
	  return "";
	end
	for i = 1, #tableofItems do
	  if string.len(tableofItems[i]) >= xMax then
		term.clear();
		term.setCursorPos(1,1);
		print (("ERROR: List item # "..i).." is too long please reduce");
		return "";
	  end
	  local x = math.floor(xMax/2) - math.floor(string.len(tableofItems[i])/2);
	  if i == material then x = x-1 end;
	  term.setCursorPos(x,y);
	  if i == material then term.write("[") end;
	  term.write(tableofItems[i]);
	  if i == material then term.write("]") end;
	  y = y+1;
	end
	local r,s = os.pullEvent("key");
	if s == 208 then material = material +1 end;
	if s == 200 then material = material -1 end;
	if material > #tableofItems then material = 1 end;
	if material < 1 then material = #tableofItems end;
	if s == 28 then
	  term.clear();
	  term.setCursorPos(1,1);
	  return tableofItems[material]
	end
  end
end
function menus()
  rednet.open("top")
  mainMenu({"Craft items"})
  craftMenu({"Tools","Utility"})
  if craftmode == 1 then
	toolMenu({"Sword","Shovel","Pickaxe","Axe","Hoe"})
	materialMenu({"Diamond","Gold","Iron","Stone","Wood"})
  elseif craftmode == 2 then
	utilMenu({"Furnace","Crafting table","Chest"})
  end
  write("Quantity: ")
  quantity = read()
end
function redn()
  if craftmode == 1 then
	if tool == 1 then
	  rednet.send(21, "sword")
	elseif tool == 2 then
	  rednet.send(21, "shovel")
	elseif tool == 3 then
	  rednet.send(21, "pickaxe")
	elseif tool == 4 then
	  rednet.send(21, "axe")
	elseif tool == 5 then
	  rednet.send(21, "hoe")
	end
	sleep(2)
	if material == 1 then
	  rednet.send(21,"diamond")
	elseif material == 2 then
	  rednet.send(21, "gold")
	elseif material == 3 then
	  rednet.send(21,"iron")
	elseif material == 4 then
	  rednet.send(21, "stone")
	elseif material == 5 then
	  rednet.send(21, "wood")
	end
  elseif craftmode == 2 then
	if util == 1 then
	  rednet.send(21,"furnace")
	elseif util == 2 then
	  rednet.send(21, "craftingtable")
	elseif util == 3 then
	  rednet.send(21, "chest")
	end
  end
  sleep(2)
  rednet.send(21,tostring(quantity))
end
function main()
  menus()
  redn()
end
main()

I'm pretty sure the error were regarding the number = tonumber(message) Pls help!
KaoS #14
Posted 05 December 2012 - 12:08 AM
erm what is the error?
milkywave1 #15
Posted 05 December 2012 - 12:56 AM
erm what is the error?
Oh yea it gives me this : craft:125: attempt to perform arithmetic __sub on number and nil
KaoS #16
Posted 05 December 2012 - 01:01 AM
and is it on the turtle or computer?
KaoS #17
Posted 05 December 2012 - 01:09 AM
I can forsee a problem with you using the variable 'number' in all of those calculations, you define it late in the program (that doesn't really matter as the function it is in executes before the others), it is the number version of a rednet message, so if you get a string not a number then it becomes nil…. "__sub on number and nil"
milkywave1 #18
Posted 05 December 2012 - 03:11 AM
and is it on the turtle or computer?
It happens on the turtle when multiplying the 'number'
and I dont quite understand what you said
KaoS #19
Posted 05 December 2012 - 03:20 AM
take a look at line 297. number=tonumber(message)

if message is 'sword' then tonumber('sword')==nil so your number variable will then be nil and cause problems with your code
milkywave1 #20
Posted 05 December 2012 - 02:44 PM
take a look at line 297. number=tonumber(message)

if message is 'sword' then tonumber('sword')==nil so your number variable will then be nil and cause problems with your code
But isn't the tonumber correct? Im not sure what went wrong still :unsure:/>
ChunLing #21
Posted 05 December 2012 - 06:48 PM
"sword" isn't a number, at least not as far as the tonumber() function is concerned. It's a string without a single digit in it, so of course the function return is nil. That is to say, there are a lot of strings that you can feed into tonumber() and get nil rather than a number back out.
milkywave1 #22
Posted 05 December 2012 - 09:17 PM
"sword" isn't a number, at least not as far as the tonumber() function is concerned. It's a string without a single digit in it, so of course the function return is nil. That is to say, there are a lot of strings that you can feed into tonumber() and get nil rather than a number back out.
But im not making "sword" a number, im making myself confused :wacko:/>
KaoS #23
Posted 05 December 2012 - 09:28 PM
look at line 297 you are making whatever you get from rednet into a number, if you just happen to get 'sword' (that's just an example, it could be anything) then everything breaks….
milkywave1 #24
Posted 06 December 2012 - 12:42 AM
look at line 297 you are making whatever you get from rednet into a number, if you just happen to get 'sword' (that's just an example, it could be anything) then everything breaks….
then how do I make it correct?
KaoS #25
Posted 06 December 2012 - 12:52 AM
number=tonumber(message) or 0

set value to 0 if tonumber(message) is nil
milkywave1 #26
Posted 06 December 2012 - 07:30 PM
number=tonumber(message) or 0

set value to 0 if tonumber(message) is nil
I tried that and it just drops the whole stack when it sucks it up from the chest