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

attempt to index ? (a nil value)

Started by TerraBAS, 30 December 2013 - 05:16 AM
TerraBAS #1
Posted 30 December 2013 - 06:16 AM
I am trying to make an auto-download script that allows me to sync user-files from a remote CC server


function startdownload(ID)
ID,path,othr = rednet.receive()
if path == "done" then
print("Exiting...")
sleep(2)
error()
elseif path ~= "/" then
shell.run("mkdir "..path)
print("Computer "..ID.." connected!")
rednet.send(ID,"path",true)
ID,flnm,othr = rednet.receive()
rednet.send(ID,"flnm",true)
ID,file,othr = rednet.receive()
filenm = path..flnm
print("Finished string: "..filenm)
flwrite = fs.open(filenm,"w")
print("Downloading...")
flwrite.write(file)
flwrite.close()
print("Download completed!")

else
print("Computer "..ID.." connected!")
rednet.send(ID,"path",true)
ID,flnm,othr = rednet.receive()
rednet.send(ID,"flnm",true)
ID,file,othr = rednet.receive()
filenm = path..flnm
print("Finished string: "..filenm)
flwrite = fs.open(filenm,"w")
print("Downloading...")
flwrite.write(file)
flwrite.close()
print("Download completed!")
end
end

But whenever I run it it says attempt to index? (a nil value) when it reaches
flwrite.write(file)

Thx for your help!
CometWolf #2
Posted 30 December 2013 - 10:51 AM
In my experience with fs this usually means your file dosen't exist. Which line is the error, and what is the string in filenm?

You might also want to use fs.exists(filepath) to check the file prior to opening it. This will return true if it exists, and otherwise false.
Edited on 30 December 2013 - 10:27 AM
TerraBAS #3
Posted 30 December 2013 - 11:21 AM
The program is fine but I was sending a nil with the server because of an programming fail XD


# can be closed