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

[Lua] [Error] "attempt to call nil" fs.close variables

Started by mibac138, 25 November 2012 - 07:56 AM
mibac138 #1
Posted 25 November 2012 - 08:56 AM
Code:

local zm = 'zmienne'
term.clear()
term.setCursorPos(1,1)
print "Status Wlaczania: "
if fs.exists(zm) then
  term.setCursorPos(1,2)
  print "Zmienne [X]"
 else
  term.setCursorPos(1,2)
  print "Zmienne [ ]"
  fs.open(zm, "w")
  fs.close(zm)
  term.setCursorPos(1,2)
  sleep(1)
  term.clearLine()
  print "Zmienne [X]"
end
Error:
startup:12: attempt to call nil

zmienne is in polish in english it means variables :D/>/>
dissy #2
Posted 25 November 2012 - 09:01 AM
fs.open returns a file handle, which is what needs passed to close, not the file name.

local h = fs.open(zm, "w")
h.close()
exploder #3
Posted 25 November 2012 - 09:04 AM
Code:
local zm = 'zmienne' term.clear() term.setCursorPos(1,1) print "Status Wlaczania: " if fs.exists(zm) then term.setCursorPos(1,2) print "Zmienne [X]" else term.setCursorPos(1,2) print "Zmienne [ ]" fs.open(zm, "w") fs.close(zm) term.setCursorPos(1,2) sleep(1) term.clearLine() print "Zmienne [X]" end
Error: startup:12: attempt to call nil zmienne is in polish in english it means variables :D/>/>/>/>

Maybe try this:


local zm = 'zmienne'
term.clear()
term.setCursorPos(1,1)
print "Status Wlaczania: "
if fs.exists(zm) then
  term.setCursorPos(1,2)
  print "Zmienne [X]"
else
  term.setCursorPos(1,2)
  print "Zmienne [ ]"
  fs.open(zm, "w")
  fs.close()
  term.setCursorPos(1,2)
  sleep(1)
  term.clearLine()
  print "Zmienne [X]"
end

Tell me if that's what you wanted.
mibac138 #4
Posted 25 November 2012 - 09:06 AM
OK but when i'm editing file "zmienne" and if i'm click any button it's writing

edit:363: bad argument: string expected, got nil
dissy #5
Posted 25 November 2012 - 09:20 AM
Maybe try this:

That is still the same incorrect usage of fs.close. See my above post.
mibac138 #6
Posted 25 November 2012 - 09:39 AM
This dont work…
dissy #7
Posted 25 November 2012 - 10:00 AM
My code is tested and verified, and it works.
If you are getting an error, we need to see both the error and the code to help.
mibac138 #8
Posted 25 November 2012 - 11:03 AM








Maybe give me yours code
dissy #9
Posted 25 November 2012 - 11:12 AM
My code was

h = fs.open("newfile", "w")
h.close()

It creates the new file, closes the file handle, and no errors.

remiX #10
Posted 25 November 2012 - 01:33 PM
Is the file in rom? If it is, you can't delete it
ChunLing #11
Posted 26 November 2012 - 07:39 AM
Post the current version of the code, incorporating the repair suggested by dissy, and the error message for that version. Don't tell us you've got new error messages without posting the code that produced them.