Jump to content




Saving Variables


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

#1 Csstform

  • Members
  • 410 posts
  • LocationU.S.A.

Posted 10 December 2013 - 10:31 AM

I'm trying to write a system that will save and load a password, username, ect. However, I cannot seem to get past these errors:

--This is the start of a Terminal Glasses OS
--All original code is by Castform
--2013-2014

local user = "admin" --this initializes the variable

if fs.isDir("/tungsten") then --checks for directory for the OS
--don't need to do anything
else
	fs.makeDir("/tungsten") -- if not there, creates the directory
end

if fs.exists("/tungsten/username") then
	local user = fs.read("/tungsten/username","r") --throws error here
	--tungstenos:14: attempt to call nil
	print(user)
else
	print("Please enter your desired username...") --notification to player
	local user = read() --read the input
	local userHandle = fs.open("/tungsten/username","w") --open file in write mode
	userHandle.write(user) --write user
	print(user) --this is to make sure the user variable being null is not the problem (it isn't)
	userhandle.close() --also throws error here
	--tungstenos:22:attempt to index ? (a nil value)
end

print(user)

Please help!

#2 Goof

  • Members
  • 751 posts

Posted 10 December 2013 - 10:47 AM

View PostCastform, on 10 December 2013 - 10:31 AM, said:

I'm trying to write a system that will save and load a password, username, ect. However, I cannot seem to get past these errors:

--This is the start of a Terminal Glasses OS
--All original code is by Castform
--2013-2014

local user = "admin" --this initializes the variable

if fs.isDir("/tungsten") then --checks for directory for the OS
--don't need to do anything
else
	fs.makeDir("/tungsten") -- if not there, creates the directory
end

if fs.exists("/tungsten/username") then
	local user = fs.read("/tungsten/username","r") --throws error here
	--tungstenos:14: attempt to call nil
	print(user)
else
	print("Please enter your desired username...") --notification to player
	local user = read() --read the input
	local userHandle = fs.open("/tungsten/username","w") --open file in write mode
	userHandle.write(user) --write user
	print(user) --this is to make sure the user variable being null is not the problem (it isn't)
	userhandle.close() --also throws error here
	--tungstenos:22:attempt to index ? (a nil value)
end

print(user)

Please help!

you have a few errors.... First of all: Its fs.open("filepath", "howtoopen") -- replace howtoopen with "r" (read-only), "w" write, and "a" for append mode

edited your code a bit:
--This is the start of a Terminal Glasses OS
--All original code is by Castform
--2013-2014

local user = "admin" --this initializes the variable

if fs.isDir("/tungsten") then --checks for directory for the OS
--don't need to do anything
else
	fs.makeDir("/tungsten") -- if not there, creates the directory
end

if fs.exists("/tungsten/username") then
	local openuser = fs.open("/tungsten/username","r") -- open the file in READ-mode
	local user = openuser.readLine() -- make the fs.open, read the line 
    openuser.close() -- close the fs.open
	print(user)
else
	print("Please enter your desired username...") --notification to player
	local user = read() --read the input
	local userHandle = fs.open("/tungsten/username","w") --open file in write mode
	userHandle.writeLine(user) --write user
	print(user) --this is to make sure the user variable being null is not the problem (it isn't)
	userHandle.close() -- userhandle  = error you need to write the exact same name as the variable! LUA is Case sensitive.
-- edited to userHandle.close()
end

print(user)


this code works fine

Edited by Mikk809h, 10 December 2013 - 10:48 AM.


#3 Csstform

  • Members
  • 410 posts
  • LocationU.S.A.

Posted 10 December 2013 - 11:48 AM

Kudos to you!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users