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

Attemp to call nil, need help with code

Started by eriqq, 14 April 2013 - 03:46 AM
eriqq #1
Posted 14 April 2013 - 05:46 AM
I've just made my first program in my mining turtle.I have a problem when tried to start , I can first enter my distance but then this pops up roedgud:12:Attempt to call nil I've tried to find my problem on the internet but it's hard to find it.

Here's the code


local running = true
local distance = 1
print ("Distance:")
local maxdistance = read("*")
print("hello")
function diggo()
if turtle.forward() == false then
turtle.dig()
end
end

function left()
turtle.turnleft()
diggo()
turtle.forward()
end
function right()
turtle.turnright()
diggo()
turtle.forward()
end

function up ()
if turtle.up() == false then
turtle.digUp()
end
turtle.up()
diggo()
end

function down()
if turtle.down() == false then
turtle.digDown()
end
turtle.down()
diggo()
end

function mining()
diggo()
left()
turtle.turnright()
diggo()
up()
right()
turtle.turnleft()
diggo()
right()
turtle.turnleft()
diggo()
down()
left()
turn.left()
end

while running==true do

mining()
distance = distance+1
if distance > maxdistance then
running = false
end
end
edit: here are the error message "roedgud:12:Attempt to call nil"
It's working now, thanks
Cranium #2
Posted 14 April 2013 - 05:50 AM
Are you running this on a computer or CC-Emu? Because that is the only reason that line 7 would return nil.
Engineer #3
Posted 14 April 2013 - 05:52 AM
For future reference, please give us the full error message. Espacially since the first part is really important.

And also, put your code within code tags: [.code] [./code] without the dots.
Formatted code with some errors:
Spoiler

local running = true
local distance = 1
print ("Distance:")
local maxdistance = read("*")
print("hello")

function diggo()
	if turtle.forward() == false then
		turtle.dig()
	end
end

function left()
	turtle.turnleft() -- !! turtle.turnLeft() !!
	diggo()
	turtle.forward()
end
function right()
	turtle.turnright() -- !! turtle.turnRight() !!
	diggo()
	turtle.forward()
end

function up()
	if turtle.up() == false then
		turtle.digUp()
	end
	turtle.up()
	diggo()
end

function down()
	if turtle.down() == false then
		turtle.digDown()
	end
	turtle.down()
	diggo()
end

function mining()
	diggo()
	left()
	turtle.turnright() -- !! turtle.turnRight() !!
	diggo()
	up()
	right()
	turtle.turnleft() -- !! turtle.turnLeft() !!
	diggo()
	right()
	turtle.turnleft() -- !! turtle.turnLeft() !! 
	diggo()
	down()
	left()
	turn.left() -- !! turtle.turnLeft() !!
end

while running==true do

	mining()
	distance = distance+1
	if distance > maxdistance then
		running = false
	end
end
Yup, Lua is CaSe SeNsEtIvE

Edit: Spotted more errors
rubyrubyrubyruby #4
Posted 14 April 2013 - 07:18 AM
and if you do this


local maxdistance = read("*")
maxdistance = tonumber(maxdistance)
print("hello")