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

Saving Variables

Started by Csstform, 10 December 2013 - 09:31 AM
Csstform #1
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!
Goof #2
Posted 10 December 2013 - 10:47 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!

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 on 10 December 2013 - 09:48 AM
Csstform #3
Posted 10 December 2013 - 11:48 AM
Kudos to you!