Jump to content




attempt to index ? (a nil value)


  • You cannot reply to this topic
9 replies to this topic

#1 bbaayyeerr

  • Members
  • 14 posts
  • LocationSweden

Posted 21 June 2015 - 11:02 PM

Hello guys!
I would like to start of with saying that I've reed the "Read this post before asking questions", since my error message is covered quite early in that post, but i still can not find a solution. Everything seams fine in my eyes, but then again I've been staring at this error trying to figure out a solution, for the last hour or so... I would be happy if there were someone that could explain what I'm doing wrong! :)/>

The error message mark the line "file.write(textutils.seri...)" as the source of failure. ("attempt to index ? (a nil value)")

dir = "client/main"

function saveUserInfo(dir)
	local username = {"user1", "user2", "user3"}  -- Here you need to define all defaul usernames
	local password = {"password1", "password2", "password3"}  -- Here you define what password each default user has

	local file = fs.open("UserInfo/"..dir, "w")
	print("Successfully opened file...") --debugging
	file.write(textutils.serialize(username).."\n"..textutils.serialize(password))
	file.close()

Thanks in advance!
//bbaayyeerr

Edited by bbaayyeerr, 22 June 2015 - 12:16 AM.


#2 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 21 June 2015 - 11:13 PM

I just tried this on a computer and it worked fine.
Check and make sure you copied everything over verbatim. You might have an error in your code but you fixed it when typing the text again into the forum.

#3 bbaayyeerr

  • Members
  • 14 posts
  • LocationSweden

Posted 21 June 2015 - 11:20 PM

View PostQuintuple Agent, on 21 June 2015 - 11:13 PM, said:

I just tried this on a computer and it worked fine.
Check and make sure you copied everything over verbatim. You might have an error in your code but you fixed it when typing the text again into the forum.

How strange! The only difference between the code that i have and the one above is that dir corresponds to dir = "client/"..data[4] where data[4] is a table with a string ("main") in the fourth position... I believe that i might have to take another look at the part that deals with what directory gets inserted into the function.

#4 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 21 June 2015 - 11:23 PM

My guess is that it didn't return a file handle, to make sure it did, try this
local file = fs.open( path, mode )
if not file then
    error( "Could not open file for writing", 2 )
end
If it errored then you've probably put in an incorrect path, like for example trying to write to a file in a folder that doesn't exist( I believe that will throw an error )

#5 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 21 June 2015 - 11:29 PM

Weird, I just ran
data={"this","is","a","test"}
dir = "client/"..data[4]
function saveUserInfo(dir)
		local username = {"user1", "user2", "user3"}  -- Here you need to define all default usernames
		local password = {"password1", "password2", "password3"}  -- Here you define what password each default user has

		local file = fs.open("UserInfo/"..dir, "w")
		print("Successfully opened file...") --debugging
		file.write(textutils.serialize(username).."\n"..textutils.serialize(password))
		file.close()
end
saveUserInfo(dir)
and it worked, created the correct directories UserInfo/client with the file inside and appeared as
{
   "user1",
   "user2",
   "user3",
}
{
   "password1",
   "password2",
   "password3",
}

View PostTheOddByte, on 21 June 2015 - 11:23 PM, said:

My guess is that it didn't return a file handle, to make sure it did, try this
Yah I started to wonder if he has something confliting like he has a file named UserInfo or UserInfo/client or a directory inside UserInfo/client/ named main

He could also do print(type(file)) just to see what it is returning.

Edited by Quintuple Agent, 21 June 2015 - 11:31 PM.


#6 bbaayyeerr

  • Members
  • 14 posts
  • LocationSweden

Posted 21 June 2015 - 11:36 PM

View PostTheOddByte, on 21 June 2015 - 11:23 PM, said:

My guess is that it didn't return a file handle, to make sure it did, try this
local file = fs.open( path, mode )
if not file then
	error( "Could not open file for writing", 2 )
end
If it errored then you've probably put in an incorrect path, like for example trying to write to a file in a folder that doesn't exist( I believe that will throw an error )

Got the error! :S Then i probably need to create the directory "client" before trying to write a file to it! ;)

I guess that i don't have to create a directory after all then...

Edited by bbaayyeerr, 21 June 2015 - 11:53 PM.


#7 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 21 June 2015 - 11:38 PM

View Postbbaayyeerr, on 21 June 2015 - 11:35 PM, said:

Got the error! :S Then i probably need to create the directory "client" before trying to write a file to it! ;)

Possibly. If that fixes it, that is great.
It is strange though, since doing fs.open("blah/halb/wubwub/test.jpg","w") should create all the of needed directories. It did when I ran your code, it created both UserInfo and UserInfo/client
Just wondering, what version of CC are you using?

Edited by Quintuple Agent, 21 June 2015 - 11:39 PM.


#8 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 21 June 2015 - 11:46 PM

View PostQuintuple Agent, on 21 June 2015 - 11:38 PM, said:

- Snip -
If he's using an older version of CC then that may be the problem, that it doesn't automatically create all the directories for you.

#9 bbaayyeerr

  • Members
  • 14 posts
  • LocationSweden

Posted 21 June 2015 - 11:50 PM

Oooh, Computercraft 1.58 didn't think of that! :unsure:/>
Sorry for the inconvenience, I should have mentioned the CC version earlier... Running FTB Monster, rather low version of the mod I would say, should probably update it now when I've realized how far bak I am in versions! xD

Edited by bbaayyeerr, 22 June 2015 - 12:21 AM.


#10 Quintuple Agent

  • Members
  • 107 posts
  • LocationThis page

Posted 22 June 2015 - 12:37 AM

Well if you need it, you could use this function to print out your data table
function checkData()
	for i,d in pairs(data) do
	 print("Indx:"..i..":"..type(d).." "..tostring(d))
end
end
and if for some reason the directories are not being created by fs.open, you can use this
function mkDirTree(tempDir)
    local full=""
    for cd in string.gmatch(tempDir,"%w+/") do
        full=full..cd
        if not fs.exists(full) then
            fs.makeDir(full)
            print("Made "..full)
        else
            print(full.." Exists")
        end
    end
return
end
Just make sure when you call it you do
mkDirTree("UserInfo/"..dir)
instead of
mkDirTree(dir)
(I forgot about that for like 3 minutes and was wondering why it was just creating client and not the UserInfo <_< )

Edited by Quintuple Agent, 22 June 2015 - 12:40 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users