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

Attempt to index ? a nil value

Started by Creeper9207, 21 February 2015 - 07:06 PM
Creeper9207 #1
Posted 21 February 2015 - 08:06 PM

--Creeper9207 Code: ftp server--
local tArgs = { ... }
rednet.open("back")
function getlist(tdir)
-- Get all the files in the directory
local sDir = shell.dir()
if tdir ~= nil then
	sDir = shell.resolve( tdir )
end

-- Sort into dirs/files, and calculate column count
local tAll = fs.list( sDir )
local tFiles = {}
local tDirs = {}

for n, sItem in pairs( tAll ) do
	if string.sub( sItem, 1, 1 ) ~= "." then
		local sPath = fs.combine( sDir, sItem )
		if fs.isDir( sPath ) then
			table.insert( tDirs, sItem )
		else
			table.insert( tFiles, sItem )
		end
	end
end
table.sort( tDirs )
table.sort( tFiles )
	
	rednet.broadcast("dirs: " .. textutils.serialize(tDirs) .. "files: " .. textutils.serialize(tFiles) .. ":text", "incoming")
end
function transferFile(xFile)
j = string.match(xFile, "(%b::)/>/>/>")
j = string.gsub(j, ":", "")
print(j)
xFile = xFile:gsub(":" .. j .. ":", "")

z = fs.open(xFile, "r")
y = z.readAll()										    --ERRORING LINE
z.close()
rednet.broadcast(y .. ":fileincoming:<" .. xFile .. ">", "incoming")
end
function recieveFile(mFile)
g = string.match(mFile, "(%b<>)")
f = string.match(mFile, "(%b::)/>/>/>")
f = f:gsub(":", "")
uFile = mFile:gsub(":" .. f .. ":", "")
uFile = uFile:gsub("<Rec>", "")
ow = fs.open(f, "w")
ow.write(uFile)
ow.flush()
ow.close()
rednet.broadcast("Success:text", "incoming")

end
while true do
senderID, msg, dis, protocol = rednet.receive("outgoing")
msgp = string.match(msg, "(%b<>)")
if msgp == "<dir>" then
msg = msg:gsub(msgp, "")
print("CMD $DIR " .. msg)
getlist(msg)
elseif msgp == "<Trans>" then
msg = msg:gsub(msgp, "")
print("CMD $TRANS " .. msg)
transferFile(msg)
elseif msgp == "<Rec>" then
print("CMD $TRANS " .. msg)
recieveFile(msg)

end
end
line 38 errors "attempt to index ? a nil value
Edited on 21 February 2015 - 07:09 PM
Anavrins #2
Posted 21 February 2015 - 08:17 PM
It means that "z = fs.open(sFile, "r")" did not return anything, either because the file doesn't exist, or your gsub pattern isn't doing what you think it's doing.
Creeper9207 #3
Posted 21 February 2015 - 08:20 PM
solved, but now in the client fOutput = fs.open(msgp, "w")
fOutput.write(fdata) is nil
Anavrins #4
Posted 21 February 2015 - 09:18 PM
Can't help you if you don't post your client code.