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

attempt to index ? (a nil value)

Started by Micheal Pearce, 17 April 2013 - 11:48 AM
Micheal Pearce #1
Posted 17 April 2013 - 01:48 PM
when i was testing this code it worked just fine but when i add Gname = "C#" to the after ever Cname if becomes broke and im confuesed because it worked just fine with the Cname

Px = 1
Py = 3
Cname = "Computer 1"
Gname = "C1"
Coms()
Cname = "Computer 2"
Coms()
Cname = "Computer 3"
Coms()
Cname = "Computer 4"
Coms()
Cname = "Computer 5"
Coms()
Cname = "Computer 6"
Coms()
Cname = "Computer 7"
Coms()
Cname = "Computer 8"
Coms()
Cname = "Computer 9"
Coms()
Px = 13
Py = 3
Cname = "Computer 10"
Coms()
the C# gets plused by one everytime
so it would look something like this

Px = 1
Py = 3
Cname = "Computer 1"
Gname = "C1"
Coms()
Cname = "Computer 2"
Gname = "C2"
Coms()
Cname = "Computer 3"
Gname = "C3"
Coms()
Cname = "Computer 4"
Gname = "C4"
Coms()
SuicidalSTDz #2
Posted 17 April 2013 - 01:50 PM
Where is the function Coms() being defined?
Micheal Pearce #3
Posted 17 April 2013 - 01:52 PM
this isnt the hole code its just the part thats messing up but heres coms()

function Coms()
file = io.open(Gname,"r")
Cstatus = file:read()
file:close()
term.setCursorPos(Px,Py)
term.setBackgroundColor(colors.lightGray)
write("+"..Cname)
if Cstatus == "ON" then
term.setBackgroundColor(colors.lime)
elseif Cstatus == "OFF" then
term.setBackgroundColor(colors.red)
end
write(" ")
Py = Py + 2
end
SuicidalSTDz #4
Posted 17 April 2013 - 01:55 PM
Which line number does it error on/ attempt to call a nil value?
Micheal Pearce #5
Posted 17 April 2013 - 01:56 PM

function main()
rednet.open("top")
Background()
PutComsLeft()
--MainSelect()
end
function Com1()
end
function Coms()
file = io.open(Gname,"r")
Cstatus = file:read()
file:close()
term.setCursorPos(Px,Py)
term.setBackgroundColor(colors.lightGray)
write("+"..Cname)
if Cstatus == "ON" then
term.setBackgroundColor(colors.lime)
elseif Cstatus == "OFF" then
term.setBackgroundColor(colors.red)
end
write(" ")
Py = Py + 2
end
function PutComsLeft()
Px = 1
Py = 3
Cname = "Computer 1"
Gname = "C1"
Coms()
Cname = "Computer 2"
Gname = "C2"
Coms()
Cname = "Computer 3"
Coms()
Cname = "Computer 4"
Coms()
Cname = "Computer 5"
Coms()
Cname = "Computer 6"
Coms()
Cname = "Computer 7"
Coms()
Cname = "Computer 8"
Coms()
Cname = "Computer 9"
Coms()
Px = 13
Py = 3
Cname = "Computer 10"
Coms()
end
function SyncCom()
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setTextColor(colors.lime)
write("Computer ID: ")
SyncID = read()
write("Syncing Computer")
textutils.slowPrint("...",1)
rednet.send(217,"PasswordPLZ")
id , PASS, dis = rednet.receive()
print(dis)
if PASS == "8932" then
end
end
function Background()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.gray)
print("SteamPunk Servers v1.5							 ")
term.setBackgroundColor(colors.blue)
print("GREEN = ON, RED = OFF")
end
function MainSelect()
event, button,X,Y = os.pullEvent("mouse_click")
if button == 1 then
if X == 1 and Y == 2 then
end
end
end
function CheckComs()
rednet.open("top")
end
main()
i believe line 15 when i add the Gname = "C#"
Engineer #6
Posted 17 April 2013 - 02:00 PM
Cname is not defined before the function, to fix this, you should make a variable at the very top of your code:
local Cname = ''''
SuicidalSTDz #7
Posted 17 April 2013 - 02:02 PM
Ninja'd
Micheal Pearce #8
Posted 17 April 2013 - 02:03 PM
Cname is not defined before the function, to fix this, you should make a variable at the very top of your code:
local Cname = ''''
that made it go from line 15 to line 16
Micheal Pearce #9
Posted 17 April 2013 - 02:04 PM
Ninja'd
moved it and the error moved to line 10
Sammich Lord #10
Posted 17 April 2013 - 02:10 PM
Gname isn't a string.
SuicidalSTDz #11
Posted 17 April 2013 - 02:13 PM
Edit: Heh, damn you and your keen eye Human :P/>
Sammich Lord #12
Posted 17 April 2013 - 02:16 PM
Edit: Heh, damn you and your keen eye Human :P/>
After raging for months you get used to looking at errors :P/>
Micheal Pearce #13
Posted 17 April 2013 - 02:18 PM
so how do i fix it now? how do i change it to a string if its not a string already
SuicidalSTDz #14
Posted 17 April 2013 - 02:19 PM
Well that's the thing, my code rarely ever errors :P/>
Engineer #15
Posted 17 April 2013 - 02:21 PM
Well that's the thing, my code rarely ever errors :P/>/>
It only gives sometimes unexpected behaviour ;)/>

You can fixt it by putting at the top:

local Gname = ''''
Sammich Lord #16
Posted 17 April 2013 - 02:21 PM
so how do i fix it now? how do i change it to a string if its not a string already
It isn't even defined. You need to define it.
Micheal Pearce #17
Posted 17 April 2013 - 02:26 PM
so how do i fix it now? how do i change it to a string if its not a string already
It isn't even defined. You need to define it.
i put a local Gname = "" at the top of the code before all the functions and stuff if thats even defining it
Sammich Lord #18
Posted 17 April 2013 - 02:28 PM
so how do i fix it now? how do i change it to a string if its not a string already
It isn't even defined. You need to define it.
i put a local Gname = "" at the top of the code before all the functions and stuff if thats even defining it
You are opening a file. Gname should be a path like "/path/to/file".
Engineer #19
Posted 17 April 2013 - 02:29 PM
Lol, herpderp, didnt even see that. I think its time to go sleep, 2:30 am here..
Micheal Pearce #20
Posted 17 April 2013 - 02:31 PM
so how do i fix it now? how do i change it to a string if its not a string already
It isn't even defined. You need to define it.
i put a local Gname = "" at the top of the code before all the functions and stuff if thats even defining it
You are opening a file. Gname should be a path like "/path/to/file".
like this
Gname = "/C1"
because that didnt work
this is starting to be weird to me because i did this

Px = 1
Py = 3
Gname = ""
Cname = "Computer 1"
Gname = "C1"
Coms()
and had a Gname = "" at the start of the function and it changed just fine on that one but after the Coms() it just breaks
SuicidalSTDz #21
Posted 17 April 2013 - 02:33 PM
Lol, herpderp, didnt even see that. I think its time to go sleep, 2:30 am here..
That sounds like fun, only 8:39 Pm here <_</>
Micheal Pearce #22
Posted 17 April 2013 - 02:34 PM
ok so i did some testing and its not the changing thats breaking

Px = 1
Py = 3
Cname = "Computer 1"
Gname = "/C1"
Coms()
print("G") -- it will print this
Cname = "Computer 2"
print("G") -- it will print this
Gname = "/C2"
print(Gname) -- it will print this
Coms() -- and it breaks here
Micheal Pearce #23
Posted 17 April 2013 - 02:55 PM
i guess i can do a if then thing in the Coms() function
Micheal Pearce #24
Posted 17 April 2013 - 03:05 PM
ok so i fixed it very simple i just needed to make the files that it was trying to bring up because they didnt exist