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

[error]Whats wrong about this code?

Started by smurfofiel, 09 May 2013 - 04:40 PM
smurfofiel #1
Posted 09 May 2013 - 06:40 PM

--Wraps the enchanting peripheral on the right side
m = perhipheral.wrap("right")

--Sets the automatic XP collection to true
m.setAutoCollect(true)

--Sets some variables
--Variable named level
level = 0
--Varaible named books based of the number of items in slot
books = turtle.getItemCount(1)

--Sets the variable level to the amount of levels the turtle has
level = m.getLevels()

--The code to enchant a book if the variable level is higher as 30
if level > 30 then
  turtle.select(1)
  print("level is higher as 30")
  if books > 0 then
	books = books - 1
	turtle.drop(books)
  end
  m.enchant(30)
  turtle.drop()
end
W00dyR #2
Posted 09 May 2013 - 06:43 PM
Using the CODE tags makes this a lot more easy to read..
Bubba #3
Posted 09 May 2013 - 07:57 PM
What error are you getting?
PixelToast #4
Posted 09 May 2013 - 07:58 PM
what is the error?
DiamondQ #5
Posted 09 May 2013 - 09:02 PM
Well, the obvious problem would be the spelling mistake on the first line:
m = perhipheral.wrap("right")
which should be
m = peripheral.wrap("right")

Secondly, since I believe you can only enchant a book if it's the only one in the slot, so if you have more than one book, it won't work.

if (books > 1) then
   turtle.transferTo(2, books - 1)
end

I think that was what you were trying to do with this section:
if (books > 0) then
but a. you only need to do it if there's more than 1, and b. transferTo keeps it in your inventory, instead of littering the ground.

So, all fixed up, you get something like (and via pastebin get brY5XcT7 enchanter)


--Wraps the enchanting peripheral on the right side
m = peripheral.wrap("right")

--Sets the automatic XP collection to true
m.setAutoCollect(true)

--Sets some variables
--Variable named level
level = 0
--Variable named books based of the number of items in slot
books = turtle.getItemCount(1)

--Sets the variable level to the amount of levels the turtle has
level = m.getLevels()

--The code to enchant a book if the variable level is higher as 30
if level > 30 then
  turtle.select(1)
  print("level is higher as 30")
  if (books > 1) then
	turtle.transferTo(2, books - 1)
  end
  m.enchant(30)
  turtle.drop()
  if (books > 1) then
	turtle.select(2)
	turtle.transferTo(1, books - 1)
  end
end

Of course, you can continue to expand it by wrapping it in a loop, so that it can collect XP. As it is, it just runs once, and if the level is not greater than 30, then it just exits.