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

3X3:4: attempt to call nil

Started by code_noob, 05 May 2013 - 11:44 AM
code_noob #1
Posted 05 May 2013 - 01:44 PM
I'm working on a 3x3 mining code with a turtle, here is my code.

turtle.dig()
turtle.up()
turtle.dig()
turtle.left()
turtle.dig()
turtle.down()
turtle.dig()
turtle.down()
turtle.dig()
turtle.right()
turtle.dig()
turtle.up()
turtle.up()
turtle.dig()
turtle.right()
turtle.dig()
turtle.down()
turtle.dig()
turtle.down()
turtle.dig()




but it doesn't work the error it gives me is : 3X3:4: attempt to call nil, I'm very new to computer craft, and have no idea what it means, please help?
Lyqyd #2
Posted 05 May 2013 - 11:25 PM
Split into new topic.

Use turtle.turnLeft() and turtle.turnRight().
code_noob #3
Posted 06 May 2013 - 09:09 AM
thanks, another question now that it's working, is there a loop I can code into it that I can stop easily? like if I do

x = 0
while x < 20 do
(actions)


it loops and I cant stop it I have to hols ctrl S or ctrl R but I'd like to type something and it just stops, if possible
GravityScore #4
Posted 06 May 2013 - 09:33 AM
thanks, another question now that it's working, is there a loop I can code into it that I can stop easily? like if I do

x = 0
while x < 20 do
(actions)


it loops and I cant stop it I have to hols ctrl S or ctrl R but I'd like to type something and it just stops, if possible

Loops in this case would only be useful if you wanted to repeat the code over and over again. The easiest way I can think of to stop the code when you want would be to use coroutines. Try this:

local function actions()

turtle.dig()
turtle.up()
turtle.dig()
turtle.left()
turtle.dig()
turtle.down()
turtle.dig()
turtle.down()
turtle.dig()
turtle.right()
turtle.dig()
turtle.up()
turtle.up()
turtle.dig()
turtle.right()
turtle.dig()
turtle.down()
turtle.dig()
turtle.down()
turtle.dig()
end

local function exit()
  while true do
	local e, but = os.pullEvent("key")
	if but == keys.e then break end
  end
end

parallel.waitForAny(actions, exit)

If you press e, it should stop the actions.
code_noob #5
Posted 06 May 2013 - 09:35 AM
I'll try this, thank you
theoriginalbit #6
Posted 06 May 2013 - 09:38 AM
x = 0
while x < 20 do
(actions)
An extra note since what Grav said is pretty much all I can think of too.
instead of defining a variable (x) and then comparing it so the loop runs forever, just do this:

while true do
  -- # code here
end
the code inside of this while loop will run forever, since while loops only run when the condition is true, and true IS true, it will never end.
code_noob #7
Posted 06 May 2013 - 09:53 AM
3x3 :39: attempt to index ? (a nil value) line 39 is parallels.waitForAny(actions,exit)
theoriginalbit #8
Posted 06 May 2013 - 09:54 AM
3x3 :39: attempt to index ? (a nil value) line 39 is parallels.waitForAny(actions,exit)
Its parallel. not parallels. seems Grav made a typo.
GravityScore #9
Posted 06 May 2013 - 10:18 AM
3x3 :39: attempt to index ? (a nil value) line 39 is parallels.waitForAny(actions,exit)
Its parallel. not parallels. seems Grav made a typo.

Oops :P/> Fixed the post. Thanks for pointing it out BIT :)/>