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

Trying to read a table and make it display the username.

Started by vyse, 09 April 2018 - 04:06 PM
vyse #1
Posted 09 April 2018 - 06:06 PM
Hey there, I'm having trouble with reading a file and making it a variable so I can display the username.

{
  {
	 name = "Vyse",
	 pass = "test",
   },
}
how the table is inside the file.

So to give you more info I have a password system I'm trying to create with an ability to ask for one's name and pass. which then are put on the table like so.

The trouble I'm having is actually making the serialized table become variable I can use in print or a statement.


local function SecureV()
	term.setCursorPos(2, 19)
   write("username"..Tbe.user)
end
local function reg( user )
  local tbl = {}
			 table.insert(tbl, user)
			  local f = fs.open("saves/USERS.txt", "w")
			   f.write(textutils.serialize(tbl))
			   f.close()
end
local function Regist()
  if fs.exists("saves/USERS.txt") == false then
		local user = {}
		  user.name = read()
		  user.pass = read("*")
		reg( user )
  else
	print('starting..")
  end
end


local function login()
  
		  local s = fs.open("saves/USER.txt", "r")
		   Tbe = textutils.unserialize(s.readAll())
		   s.close()
			local Tbe = {}
				 local user1 = Tbe.user
				 local pass1 = Tbe.pass
			local password = read("*")
		 if password == pass1 then
				 redstone.setOutput("right", true)
				 sleep(2)
				  redstone.setOutput("right", false)
		else
		  print("incorrect")
	   end
end

Regist()
login()

I took out what I thought I could use help or some advice on. I'm honestly just trying to get the name, copy it into a table named USERS.txt then go open it with it only asking for a password, and displays the name.

any help would be appreciated :)/>
Edited on 09 April 2018 - 04:16 PM
EveryOS #2
Posted 09 April 2018 - 06:12 PM
Mismatched quotes, Tbe vs tbe is case-sensitive, you are defining tbe locally but trying to access it in a function defined before the variable (outside scope), et cetera, et cetera
vyse #3
Posted 09 April 2018 - 06:15 PM
Mismatched quotes, Tbe vs tbe is case-sensitive, you are defining tbe locally but trying to access it in a function defined before the variable (outside scope), et cetera, et cetera
So what your saying is I should have Tbe = Tbe instead of making different ones like Tbe and tbe. see I wasn't sure what to do there soI just started fiddling around but it's not going so well haha.

Edit: so I've changed some things around. like taking out the username part for right now and it seems the password part fails every time. so I'm not sure how reading the table is supposed to work.
I have looked at many types of ways to do it but I can't get it right for some reason.
Edited on 09 April 2018 - 04:25 PM
EveryOS #4
Posted 09 April 2018 - 06:27 PM

local Tbe = {}
local function SecureV()
   term.setCursorPos(2, 19)
   write("username"..Tbe.name)
end
local function reg( user )
  local f = fs.open("saves/USERS.txt", "w")
  f.write(textutils.serialize({user}))
  f.close()
end
local function Regist()
  if not fs.exists("saves/USERS.txt") then
	local user = {}
	write("Creating new profile\nUsername: ")
	user.name = read()
	print("Password:")
	user.pass = read("*")
	reg( user )
  else
	print("starting..")
  end
end
local function login()
  local s = fs.open("saves/USER.txt", "r")
  Tbe = textutils.unserialize(s.readAll())
  s.close()
   local user1 = Tbe.name
   local pass1 = Tbe.pass
   local password = read("*")
	 if password == pass1 then
	   SecureV()
	   redstone.setOutput("right", true)
	   sleep(2)
	   redstone.setOutput("right", false)
	 else
		print("incorrect")
	 end
end
Regist()
login()
Try this
Edited on 09 April 2018 - 04:46 PM
vyse #5
Posted 09 April 2018 - 06:38 PM

local Tbe = {}
local function SecureV()
   term.setCursorPos(2, 19)
   write("username"..Tbe.user)
end
local function reg( user )
  local f = fs.open("saves/USERS.txt", "w")
  f.write(textutils.serialize({user}))
  f.close()
end
local function Regist()
  if not fs.exists("saves/USERS.txt") then
	local user = {}
	write("Creating new profile\nUsername: ")
	user.name = read()
	print("Password:")
	user.pass = read("*")
	reg( user )
  else
	print("starting..")
  end
end
local function login()
  local s = fs.open("saves/USER.txt", "r")
  Tbe = textutils.unserialize(s.readAll())
  s.close()
   local user1 = Tbe.user
   local pass1 = Tbe.pass
   local password = read("*")
	 if password == pass1 then
	   SecureV()
	   redstone.setOutput("right", true)
	   sleep(2)
	   redstone.setOutput("right", false)
	 else
		print("incorrect")
	 end
end
Regist()
login()
Try this
Hmmm.. I get this..
startup:15: attempt to concentrate string and nil – this is the write("username:"..Tbe.user)

edit: I was able to successfully login when i took the write("username"..Tbe.user) out. and it successfully stored the data. but now i just need to read the username properly.
Edited on 09 April 2018 - 04:43 PM
EveryOS #6
Posted 09 April 2018 - 06:47 PM
I've modified it, try the new code (which i directly edited to my previous comment), you will first have to delete saves/user.txt
Edited on 09 April 2018 - 04:47 PM
vyse #7
Posted 09 April 2018 - 06:58 PM
I've modified it, try the new code, you will first have to delete saves/user.txt
Thank you! i got it to work. so the problem was i didnt have a way to read the table in SecureV() so I just went ahead and put

local u = fs.open("saves/USER.txt", "r")
local Mt = textutils.unserialize(u.readAll())
u.close()
local name = Mt.name
and works great now thank you!