Jump to content




attempt to index ? (a nil value)


7 replies to this topic

#1 NeonPhoenix

  • New Members
  • 4 posts

Posted 10 June 2012 - 11:26 AM

I am trying to create a auto-crafting system and i am currently having a small problem with this code


Spoiler



every time i run this script i keep getting
torch:21: attempt to index ? (a nil value)

please help

#2 Cloudy

    Ex-Developer

  • Members
  • 2,543 posts

Posted 10 June 2012 - 11:52 AM

Change all references to the "color" API to "colors".

#3 NeonPhoenix

  • New Members
  • 4 posts

Posted 10 June 2012 - 12:52 PM

Thanks that seem to have worked

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,454 posts

Posted 10 June 2012 - 05:04 PM

Also, you really should return tonumber(amount) in function getInput and then compare the number to actual numbers, not string literals.

#5 blipman17

  • Members
  • 92 posts

Posted 11 June 2012 - 07:09 PM

i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler


#6 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 11 June 2012 - 07:25 PM

View Postblipman17, on 11 June 2012 - 07:09 PM, said:

i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler
You should make your own post about this.
The problem is here:
if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:
if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:
local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:
fs.open("/path/to/file", "r")
to:
local file = fs.open("startupprogram.txt", "r")


#7 blipman17

  • Members
  • 92 posts

Posted 11 June 2012 - 07:34 PM

View PostMysticT, on 11 June 2012 - 07:25 PM, said:

View Postblipman17, on 11 June 2012 - 07:09 PM, said:

i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler
You should make your own post about this.
The problem is here:
if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:
if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:
local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:
fs.open("/path/to/file", "r")
to:
local file = fs.open("startupprogram.txt", "r")

View PostMysticT, on 11 June 2012 - 07:25 PM, said:

View Postblipman17, on 11 June 2012 - 07:09 PM, said:

i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler
You should make your own post about this.
The problem is here:
if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:
if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:
local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:
fs.open("/path/to/file", "r")
to:
local file = fs.open("startupprogram.txt", "r")

View PostMysticT, on 11 June 2012 - 07:25 PM, said:

View Postblipman17, on 11 June 2012 - 07:09 PM, said:

i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler
You should make your own post about this.
The problem is here:
if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:
if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:
local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:
fs.open("/path/to/file", "r")
to:
local file = fs.open("startupprogram.txt", "r")

i am going to make a new post.

about that "path/to/file"
i tried some thins on the internet, but could'nt figure it out an i think that i left it accidentally.

it is not yet solved by your change, but i know the file exists and has a value

#8 blipman17

  • Members
  • 92 posts

Posted 11 June 2012 - 07:37 PM

View PostMysticT, on 11 June 2012 - 07:25 PM, said:

View Postblipman17, on 11 June 2012 - 07:09 PM, said:

i'm having something simular,
it's my startupprogram and if there is a program called startupprogram.txt, it should be read and the programname in that program should be executed

Spoiler
You should make your own post about this.
The problem is here:
if fs.exists("startupprogram.txt")~="false" then
It returns always true, because the boolean returned by the function will always be different from the string "false". So it tries to open the file, wich doesn't exist and returns nil.
It should be:
if not fs.exists("startupprogram.txt") then
And you should always check the file handle returned by fs.open:
local file = fs.open("/path/to/file", "r")
if file then
  -- use the file
  file.close() -- don't forget to close it
else
  print("Error opening file")
end
And you probably want to change:
fs.open("/path/to/file", "r")
to:
local file = fs.open("startupprogram.txt", "r")

i changed the code but it is still not working,
i know the file exists and has "chestdispencer" written in it.

about the "path/to/file" thing, i left that accidentally when i tried things on the internet

but it is a good idea to post this as an other tread





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users