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

[Lua][Error]attempt to index(a nil value)

Started by jamzam90, 21 April 2013 - 07:53 PM
jamzam90 #1
Posted 21 April 2013 - 09:53 PM
error on line 21


local pie = ""
local x = ""
local y = ""
local t = {}
rednet.open("top")
function getDest()
  g = fs.open("dest","r")
  print(g.readAll())  
  g.close()
end
function setDest(x,y)
  t[x] = {}
  t[x][1] = y
end

function send()
  print("destonation")
  c = read()
  if c == hi[c] then
	sleep(1)
	rednet.send(tonumber(t[c]["comp"]),"open")
	sleep(1)
  end
end
function rec(o)
  if o == "open" then
	redstone.setOutput("left",true)
	sleep(2)
	redstone.setOutput("left",false)
  end
end	
getDest()
setDest(x,y)
hi[x] = {}
hi[x][1] = y
sleep(1)
while true do
  shell.run("clear")
  a,b,c,d,e = os.pullEvent()
  if a == "modem_message" then
	rec(e)
	elseif a == "key" then
	send()
  end  
end
Edited on 21 April 2013 - 08:49 PM
Engineer #2
Posted 21 April 2013 - 09:59 PM
It expects that x is a variable. Put it into quotations and then it would be fine:
t['x'] = {}
theoriginalbit #3
Posted 21 April 2013 - 10:00 PM
It expects that x is a variable. Put it into quotations and then it would be fine:
t['x'] = {}
I think x is a variable, since its in the function… bad indentation… OP please use [code][/code] tags and re-indent the code so it is more readable
jamzam90 #4
Posted 21 April 2013 - 10:02 PM
It expects that x is a variable. Put it into quotations and then it would be fine:
t['x'] = {}
I think x is a variable, since its in the function… bad indentation… OP please use [code][/code] tags and re-indent the code so it is more readable
It was Indented when i made it and copied the editor on here doesnt like tabs
Engineer #5
Posted 21 April 2013 - 10:03 PM
@BIT:
Lol, didnt even saw it.. I should have looked better though…

@OP
Are you sure you are using the parameters?
theoriginalbit #6
Posted 21 April 2013 - 10:05 PM
It was Indented when i made it and copied the editor on here doesnt like tabs
Unless it is in code tags….

now your line

setDest(x,y)
where are x and y meant to be coming from? because at the moment they are nil and that is causing your problems
jamzam90 #7
Posted 21 April 2013 - 10:05 PM
@BIT:
Lol, didnt even saw it.. I should have looked better though…

@OP
Are you sure you are using the parameters?
As far as i can tell I am.
jamzam90 #8
Posted 21 April 2013 - 10:06 PM
It was Indented when i made it and copied the editor on here doesnt like tabs
Unless it is in code tags….

now your line

setDest(x,y)
where are x and y meant to be coming from? because at the moment they are nil and that is causing your problems


the x and y are coming from the getDest() function
Engineer #9
Posted 21 April 2013 - 10:08 PM
- derping here


Well.. X abd y are never defined as variable.. Thats your problem
jamzam90 #10
Posted 21 April 2013 - 10:18 PM
Changed the Main post
Edited on 21 April 2013 - 08:30 PM
theoriginalbit #11
Posted 21 April 2013 - 10:22 PM
does the file 'dest' exist on the computer?

I suggest this code

function getDest()
  local g = fs.open('dest', 'r')
  if g then
    print(g.readAll())
    g.close()
  else
    -- the file doesn't exist, create it here and set all your values to defaults
  end
end
jamzam90 #12
Posted 21 April 2013 - 10:26 PM
the file dest does exist
theoriginalbit #13
Posted 21 April 2013 - 10:30 PM
then what line is the new error happening on?
jamzam90 #14
Posted 21 April 2013 - 10:34 PM
its happening on line 21
Espen #15
Posted 21 April 2013 - 10:46 PM
@jamzam90:
You have to post your whole code or this is a guessing game and we can't help you properly.
You don't go to your doctor with only the foot that is hurting. You always take your whole body, don't you? ;)/>
jamzam90 #16
Posted 21 April 2013 - 10:50 PM
@jamzam90:
You have to post your whole code or this is a guessing game and we can't help you properly.
You don't go to your doctor with only the foot that is hurting. You always take your whole body, don't you? ;)/>
I hadnt noticed some of it was missing
Espen #17
Posted 21 April 2013 - 10:59 PM
Do what Engineer said, i.e. replace all t[x] with t["x"].
Ort as an alternative which works the same: replace it with t.x
Do this with all the tables (hi[x] -> h["x"] or h.x)

Edit - to explain a little as to why:
fruits[abc] will access the table fruits with the key in abc.
abc is a variable here, so whatever is in abc will be the key, not "abc".
If abc == "yummy", then fruits[abc] is the same as fruits["yummy"].

Also, to make it easier to access named keys, Lua allows to replace fruits["yummy"] with fruits.yummy
Edited on 21 April 2013 - 09:05 PM