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

Why Isnt My Code Working?

Started by Th3RadMan, 13 September 2013 - 11:06 PM
Th3RadMan #1
Posted 14 September 2013 - 01:06 AM
I have this simple piece of code that isnt doing anything… it just closes the program

it should detect a mob, then attack, then go through its inventory and drop everything


local x = turtle.detect()
while true do

if x == true then
  turtle.attack()
    turtle.attack()
	  for i = 1, 16 do
    turtle.select(i)
    turtle.drop()
    end
  end
end
GravityScore #2
Posted 14 September 2013 - 03:08 AM
Firstly, you need the turtle.detect() inside the loop. Second, you need to have the loop wait for a bit every time it runs, otherwise it'll error (close the program).


while true do
  if turtle.detect() then
    turtle.attack()
    turtle.attack()
    for i = 1, 16 do
      turtle.select(i)
      turtle.drop()
    end
  end
  sleep(0.5)
end
bigbaddevil6 #3
Posted 14 September 2013 - 03:14 AM
You're suppose to put the error that you're getting. which in this case is "1: attempt to index ? (a nil value)" am I right? turtle.detect() doesn't detect mobs, only detects blocks in front of the turtle, so it doesn't need to be in the code cause it would only run if there was a block placed in front of it which is pointless.

while true do
    turtle.attack()
    turtle.attack()
    for i = 1, 16 do
	  turtle.select(i)
	  turtle.drop()
    end
  sleep(0.5)
end
Th3RadMan #4
Posted 14 September 2013 - 12:57 PM
You're suppose to put the error that you're getting. which in this case is "1: attempt to index ? (a nil value)" am I right? turtle.detect() doesn't detect mobs, only detects blocks in front of the turtle, so it doesn't need to be in the code cause it would only run if there was a block placed in front of it which is pointless.
 while true do turtle.attack() turtle.attack() for i = 1, 16 do turtle.select(i) turtle.drop() end sleep(0.5) end 

gravity score solved it for me… i didnt get an error code, and they are melee turtles… i dont think i would detect and attack with a mining turtle lol, and yes, when a melee turtle.detects it detects mobs, not blocks
GravityScore #5
Posted 14 September 2013 - 02:17 PM
You're suppose to put the error that you're getting. which in this case is "1: attempt to index ? (a nil value)" am I right? turtle.detect() doesn't detect mobs, only detects blocks in front of the turtle, so it doesn't need to be in the code cause it would only run if there was a block placed in front of it which is pointless.

while true do
	turtle.attack()
	turtle.attack()
	for i = 1, 16 do
	  turtle.select(i)
	  turtle.drop()
	end
  sleep(0.5)
end

When having a loop that never yields, sometimes (not sure under which circumstances) the computer will just shut down or the program will exit and not give an error message. Just because turtle.detect() might not detect mobs (which it does), calling it without a mob in front of the turtle won't cause an attempt to index nil - the function itself still exists (the error would occur if the function didn't exist), it'll just return false.
bigbaddevil6 #6
Posted 14 September 2013 - 04:38 PM
When it comes from the turtle.detect() I pulled that info right from the wiki. http://computercraft.info/wiki/Turtle_(API)



If it does detect mobs the wiki needs to be updated for it.
Engineer #7
Posted 14 September 2013 - 05:38 PM
When it comes from the turtle.detect() I pulled that info right from the wiki. http://computercraft...ki/Turtle_(API)



If it does detect mobs the wiki needs to be updated for it.
Turtles cannot detect mobs, just tested it to be very sure on this answer.
Th3RadMan #8
Posted 15 September 2013 - 12:28 AM
When it comes from the turtle.detect() I pulled that info right from the wiki. http://computercraft...ki/Turtle_(API)



If it does detect mobs the wiki needs to be updated for it.
Turtles cannot detect mobs, just tested it to be very sure on this answer.

what kind of turtle did you use?

for me when i have a melee turtle, it does nothing, but if myself of a mob moves in front of it, it will 'activate'
bigbaddevil6 #9
Posted 15 September 2013 - 01:13 AM
its because they will always attack, it just it won't do the animation unless there is something in front of it. I assume that is the mod itself and not part of lua.
Th3RadMan #10
Posted 15 September 2013 - 01:33 AM
ok, im assuming the turtle can only detect 1 block in front of it? if so then it shouldnt activate unless if there is a block in front of it, correct?
Inumel #11
Posted 15 September 2013 - 02:44 AM
If you just use

turtle.detect()
then yes.
But you can also detect up and down, using


turtle.detectUp()
and

turtle.detectDown()
respectively
Th3RadMan #12
Posted 15 September 2013 - 03:13 AM
If you just use

turtle.detect()
then yes.
But you can also detect up and down, using


turtle.detectUp()
and

turtle.detectDown()
respectively

yes, i know that, i mean it shouldnt even attack if a mob walks in front of it, but at least if its a melee turtle it will attack on the instance of detecting a mob
Th3RadMan #13
Posted 15 September 2013 - 04:56 AM
also, will endermen attempt to tp if attacked by a turtle?
brattus123 #14
Posted 16 September 2013 - 09:09 PM
[We already gave you your own post. Go read the sticky posts and then go find the split post. It'll be started by you, so check the topic listings. -L]
Edited on 16 September 2013 - 08:24 PM