Jump to content




[Lua][Error][Turtle]Sometimes works and Sometimes dosnt


  • You cannot reply to this topic
8 replies to this topic

#1 Sparx

  • New Members
  • 4 posts

Posted 24 December 2012 - 01:53 PM

I am trying to run this code which makes a platform, sometimes when I run it, it automatically goes to "Error: block in the way", but also sometimes it does not, the first time I ever ran it, it ran flawlessly, but I tried a second time and it failed; it also sometimes just stops and gives me the error "Error: block in the way" when there is not a block in the way.

After it gives me the "Error: block in the way" I goto the lua interpreter and type turtle.forward() but it comes up with "false" when I do it even when it should not.

I have the correct slots filled too.

local blocks = turtle.getItemCount(2)
for slot=3,16 do
  if turtle.compareTo(2) then
	blocks = blocks + turtle.getItemCount(slot)
  end
end
size = math.floor(math.sqrt(blocks))
sizeStr = tostring(size)
print("Making a " .. sizeStr .. "x" .. sizeStr .. " platform")
function prevBlockSlot(curSlot)
  for slot=curSlot,2,-1 do
	turtle.select(slot)
	if turtle.compareTo(2) then
	  return slot
	end
  end
end
function forward(curSlot)
  if turtle.getFuelLevel() < 1 then
	select(1)
	turtle.refuel(1)
	turtle.select(curSlot)
  end
  for i=1,10 do
	if turtle.detect() then
	  turtle.dig()
	end
	if turtle.forward() then
	  return true
	end
  end
  return nil
end
function placeDown(curSlot)
  if not turtle.compareTo(2) then
	curSlot = prevBlockSlot(curSlot)
  end
  turtle.placeDown()
  return curSlot
end
local curSlot = prevBlockSlot(16)
for row=1,size do
  for i=1,size-1 do
	curSlot = placeDown(curSlot)
	if not forward(curSlot) then
	  print("Error: block in the way")
	  return
	end
  end
  curSlot = placeDown(curSlot)
  if row%2 == 1 then
	turtle.turnRight()
	if not forward(curSlot) then
	  print("Error: block in the way")
	  return
	end
	turtle.turnRight()
  else
	turtle.turnLeft()
	if not forward(curSlot) then
	  print("Error: block in the way")
	  return
	end
	turtle.turnLeft()
  end
end


#2 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 24 December 2012 - 02:24 PM

i usually do something like this whenever i try to go forward:
while not turtle.forward() do turtle.dig() end


#3 Sparx

  • New Members
  • 4 posts

Posted 24 December 2012 - 04:22 PM

View PostPixelToast, on 24 December 2012 - 02:24 PM, said:

i usually do something like this whenever i try to go forward:
while not turtle.forward() do turtle.dig() end

Sorry if this a stupid question, but where would I put that if I added it.

#4 PixelToast

  • Signature Abuser
  • 2,265 posts
  • Location3232235883

Posted 24 December 2012 - 06:12 PM

in replace to turtle.forward()
it will ensure that turtle.forward() always returns true
it might crash if it reaches an unloaded chunk though

#5 ChunLing

  • Members
  • 2,027 posts

Posted 24 December 2012 - 06:17 PM

The forward function should already be doing that.
function forward(curSlot)
  if turtle.getFuelLevel() < 1 then
	    select(1)
	    turtle.refuel(1)
	    turtle.select(curSlot)
  end
  for i=1,10 do
	    if turtle.detect() then
		  turtle.dig()
	    end
	    if turtle.forward() then
		  return true
	    end
  end
  return nil
end
This function doesn't deal with mobs. Is there a chance that you're running into spawned mobs, and that's causing forward to fail?

#6 Sparx

  • New Members
  • 4 posts

Posted 25 December 2012 - 12:30 AM

View PostChunLing, on 24 December 2012 - 06:17 PM, said:

The forward function should already be doing that.
function forward(curSlot)
  if turtle.getFuelLevel() < 1 then
		select(1)
		turtle.refuel(1)
		turtle.select(curSlot)
  end
  for i=1,10 do
		if turtle.detect() then
		  turtle.dig()
		end
		if turtle.forward() then
		  return true
		end
  end
  return nil
end
This function doesn't deal with mobs. Is there a chance that you're running into spawned mobs, and that's causing forward to fail?

That could be it, but I am right next to a village but I have not had any villagers come right in front of my turtle.

#7 ChunLing

  • Members
  • 2,027 posts

Posted 25 December 2012 - 01:20 AM

And you've placed fuel in slot 1, or have enough fuel for that not to matter, right?

#8 Sparx

  • New Members
  • 4 posts

Posted 25 December 2012 - 05:55 PM

View PostChunLing, on 25 December 2012 - 01:20 AM, said:

And you've placed fuel in slot 1, or have enough fuel for that not to matter, right?

Yes, I also run into a problem that it sometimes builds a 8x8(even when I am full of cobble).

Posted Image

#9 ChunLing

  • Members
  • 2,027 posts

Posted 26 December 2012 - 02:55 PM

Hmm...could it be that one of your other mods is interfering with the turtle functions somehow? That's all I can think of.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users