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

Attempt to index ? (a nil value)

Started by tiagodias189, 23 December 2014 - 10:10 AM
tiagodias189 #1
Posted 23 December 2014 - 11:10 AM
Hello guys, I am really new to computercraft and programing alltough i really love trying to program my computer on minecraft.

As many of you should know, direwolf20 is a pretty famous person on youtube. I have been playing is 1.7.10 modpack and am currently building a spaceship. I tried to make a program to control lights and other stuff on the spaceship. I copied pretty much one of his computercraft tutorials and the button api but it keeps giving me an error. i tried to correct it but i dont know what is wrong. Please help!!

The error is : control :6: attempt to index ? (a nil value)
And this is the program : http://pastebin.com/fDEee4At

That line is pretty much exactly how it is in his video and mine reports that error.
Just one more thing. If you know any online, free and good computercraft tutorials can you give me the link so i can try to learn?

Thank you!
Tiago Dias
Cranium #2
Posted 23 December 2014 - 01:11 PM
You're loading the api called "Button", but calling an api called "button". It's just a simple capitalization issue.
tiagodias189 #3
Posted 23 December 2014 - 11:40 PM
Wow that was so easy!! Thank you very much :)/> I allready corrected all the other capitalizing errors but now i get another error on the 79 line on this program: http://pastebin.com/8fpJPWVQ but i deleted the line and it all seems to be fine now :)/> Can you tell me how do i make it do stuff? for example is i press button "x" the do this (i think i can make it do what i want by myself.. i just dont know where to write it :$)
Bomb Bloke #4
Posted 24 December 2014 - 05:45 AM
The idea is that when you create the buttons, you include pointers to the functions that you want to run when they're pressed. Like this:

local function LightsOnFunc()
	rs.setOutput("right",true)  -- Or whatever
end

local function LightsOffFunc()
	rs.setOutput("right",false)  -- Or whatever
end

local function fillTable()
	button.setTable("LightsOn", LightsOnFunc, 20,40,3,10)
	button.setTable("LightsOff", LightsOffFunc, 60,80,3,10)
	button.screen()
end

Note that the line you deleted is the one which triggers the execution of these functions. You'll want to put it back, as the buttons won't do anything without it.
tiagodias189 #5
Posted 24 December 2014 - 12:36 PM
Ok Thank you!!
I think i made it do what i want but when i put back the line you told me i had to have it still gives me the attempt to call nil error. The error is now on line 87, can you take a look?? http://pastebin.com/Rg1qWpq9
KingofGamesYami #6
Posted 24 December 2014 - 03:43 PM
As far as I can tell, that error shouldn't be thrown by your current script. However, I did notice you have two functions named the same thing:


function fillTable()
   setTable("LightsOn", LightsOn, 10, 30, 3, 8)
end

function fillTable()
   setTable("LightsOff", LightsOff, 40, 60, 3,8)  
end
Bomb Bloke #7
Posted 24 December 2014 - 09:41 PM
The code I listed above is intended to go into your main script, not into your API.
tiagodias189 #8
Posted 25 December 2014 - 10:20 AM
Is this it?? http://pastebin.com/4c4fzw3F

But i still get the error on the api "attempt to call nil"
Bomb Bloke #9
Posted 25 December 2014 - 10:28 AM
You get that error because you failed to assign any valid functions to your buttons. The second parameter you pass to Button.setTable() is the variable holding the desired function pointer. Because the variable you're supplying doesn't lead to a function, the script errors out when it attempts to run that "function".

local function LightsOnFunc()  -- ********* Here we assign a function to the variable "LightsOnFunc"...
        rs.setOutput("right",true)  -- Or whatever
end

local function LightsOffFunc()
        rs.setOutput("right",false)  -- Or whatever
end

local function fillTable()
        button.setTable("LightsOn", LightsOnFunc, 20,40,3,10)-- ********* Here we pass the "LightsOnFunc" pointer to the button-builder...
        button.setTable("LightsOff", LightsOffFunc, 60,80,3,10)
        button.screen()
end
tiagodias189 #10
Posted 25 December 2014 - 12:23 PM
I think im almost there, it even turned the redstone output on this time!!
But it still gives me an error on the next line (24) attempt to index a nil value, when i press the lights off button.
http://pastebin.com/4RpyxXn8


Sorry for sucking at this :s
The_Cat #11
Posted 25 December 2014 - 12:36 PM
You are calling the api "button" which dosnt exist. The api that you are loading in is called "Button" Capitalization is your problem here.