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

help with api: attempt to index ? (a nil value)

Started by Levi, 01 July 2013 - 05:44 AM
Levi #1
Posted 01 July 2013 - 07:44 AM
Im using the following code:


function Tree()
x = 0
	while turtle.detect() do
	 turtle.dig()
	 turtle.digup()
	 turtle.up()
	 x = x+1
	end
	turtle.up()
	while turtle.detect() do
		 turtle.dig()
		 turtle.digup()
		 turtle.up()
		 x = x+1
	end
	turtle.up()
		while turtle.detect() do
		 turtle.dig()
		 turtle.digup()
		 turtle.up()
		 x = x+1
	end
	turtle.up()
	while turtle.detect() do
	 turtle.dig()
	 turtle.digup()
	 turtle.up()
	 x = x+1
	end

	while not turtle.detectDown() do
	 turtle.down()
	end

	print("The tree was "..x.." high")
end
function TreeNext()
	turtle.turnRight()
	turtle.forward()
	turtle.turnLeft()
end
function TreeLeft()
	turtle.turnLeft()
	turtle.forward()
	turtle.turnRight()
end
function Jungle()
	T.Tree()
	T.TreeNext()
	T.Tree()
	turtle.forward()
	T.Tree()
	T.TreeLeft()
	T.Tree
	print("complete")
end


all I get is a message: attempt to index ? (a nil value)

when I restart the turtle it sais:
bios:337: [string "Tree"]:58: '=' expected

what should I do??
I tried a lot of stuff but nothing works…
I checked line 58 but all there is on that line is an end for a function
I use notepad++ for writing the code for the api

can somebody help me?
Lyqyd #2
Posted 01 July 2013 - 02:03 PM
Split into new topic.
Engineer #3
Posted 01 July 2013 - 02:15 PM
Check line 54:


T.Tree
--# Should be:
T.Tree()
Levi #4
Posted 02 July 2013 - 03:10 AM
That was already there in the original code sorry
I needed to change turtle.detectup() into turtle.detectUp() with capital U
Now the errors are gone….

But…

Now it won't go up (didn't change the turtle.up() to turtle.Up() )

Why could this be?
Engineer #5
Posted 02 July 2013 - 03:29 AM
You are declaring functions, so call them.
Or for a matter of fact, remove the functions and only do the code if you dont want to recall them anyway.

Edit: Did you check if the turtle has any fuel?
Levi #6
Posted 02 July 2013 - 06:40 AM
You are declaring functions, so call them.
Or for a matter of fact, remove the functions and only do the code if you dont want to recall them anyway.

Edit: Did you check if the turtle has any fuel?

What do you mean? Yes I call the functions…
in my turtle I type lua -> T.Jungle()
but it won't go up
when I type lua -> turtle.up() it sais false

Fuel is disabled
Engineer #7
Posted 02 July 2013 - 06:52 AM
Figured it out. It will error out in the function Jungle if the filename is not T and it is loaded as an API. You can just call functions the normal way, since you are in the same file.
So try this code:
Spoiler

function Tree()
	x = 0
	while turtle.detect() do
		turtle.dig()
		turtle.digup()
		turtle.up()
		x = x+1
	end
	turtle.up()
	while turtle.detect() do
		turtle.dig()
		turtle.digUp()
		turtle.up()
		x = x+1
	end
	turtle.up()
	while turtle.detect() do
		turtle.dig()
		turtle.digUp()
		turtle.up()
		x = x+1
	end
	turtle.up()
	while turtle.detect() do
		turtle.dig()
		turtle.digUp()
		turtle.up()
		x = x+1
	end
	while not turtle.detectDown() do
		turtle.down()
	end
	print("The tree was "..x.." high")
end
function TreeNext()
	turtle.turnRight()
	turtle.forward()
	turtle.turnLeft()
end
function TreeLeft()
	turtle.turnLeft()
	turtle.forward()
	turtle.turnRight()
end
function Jungle()
	Tree()
	TreeNext()
	Tree()
	turtle.forward()
	Tree()
	TreeLeft()
	Tree()
	print("complete")
end
Levi #8
Posted 02 July 2013 - 07:10 AM
Figured it out. It will error out in the function Jungle if the filename is not T and it is loaded as an API. You can just call functions the normal way, since you are in the same file.
So try this code:
Spoiler

function Tree()
	x = 0
	while turtle.detect() do
		turtle.dig()
		turtle.digup()
		turtle.up()
		x = x+1
	end
	turtle.up()
	while turtle.detect() do
		turtle.dig()
		turtle.digUp()
		turtle.up()
		x = x+1
	end
	turtle.up()
	while turtle.detect() do
		turtle.dig()
		turtle.digUp()
		turtle.up()
		x = x+1
	end
	turtle.up()
	while turtle.detect() do
		turtle.dig()
		turtle.digUp()
		turtle.up()
		x = x+1
	end
	while not turtle.detectDown() do
		turtle.down()
	end
	print("The tree was "..x.." high")
end
function TreeNext()
	turtle.turnRight()
	turtle.forward()
	turtle.turnLeft()
end
function TreeLeft()
	turtle.turnLeft()
	turtle.forward()
	turtle.turnRight()
end
function Jungle()
	Tree()
	TreeNext()
	Tree()
	turtle.forward()
	Tree()
	TreeLeft()
	Tree()
	print("complete")
end

Really thanks but that wasn't the problem…
The names don't have to be changed.
There was some kind of problem with the mod, I re-downloaded it and everything worked just fine…
To bad I didn't do that sooner.

Again thanks everybody!!
Engineer #9
Posted 02 July 2013 - 07:22 AM
The names don't have to be changed.

Well, if you named your program "t" and did os.loadAPI("t") your program will error out, again. This way, it will prevent that erroring and it doesnt care what name the file has. So it definitely has advantages, how you are doing it is in my opinion a bit sloppy.
I know Im not the best coder or anything, but Im pretty sure of that. So please, please change it the code I provided you. Just because you won't come back because you named it something else it attempts to call nil.
In general, its just better.
Levi #10
Posted 02 July 2013 - 01:00 PM
The names don't have to be changed.

Well, if you named your program "t" and did os.loadAPI("t") your program will error out, again. This way, it will prevent that erroring and it doesnt care what name the file has. So it definitely has advantages, how you are doing it is in my opinion a bit sloppy.
I know Im not the best coder or anything, but Im pretty sure of that. So please, please change it the code I provided you. Just because you won't come back because you named it something else it attempts to call nil.
In general, its just better.

Well I don't know really how I would do the stuff you just said…
I just created a file in the apis folder (Mods\ComputerCraft1.53\lua\rom\apis) named "T" where I put this code in
It works for me and I don't need to load anything??
PixelToast #11
Posted 02 July 2013 - 01:29 PM
remove the T: before all the function calls in jungle, they are unnecessary and will probably break
Levi #12
Posted 02 July 2013 - 01:38 PM
remove the T: before all the function calls in jungle, they are unnecessary and will probably break

already did that thanks anyway..
Anyway my api is working so I wont be needing any help anymore

thanks again