Jump to content




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

help

6 replies to this topic

#1 vyse

  • Members
  • 45 posts

Posted 09 April 2018 - 05: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 by vyse, 09 April 2018 - 05:16 PM.


#2 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 09 April 2018 - 05: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

#3 vyse

  • Members
  • 45 posts

Posted 09 April 2018 - 05:15 PM

View PostEveryOS, on 09 April 2018 - 05:12 PM, said:

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 by vyse, 09 April 2018 - 05:25 PM.


#4 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 09 April 2018 - 05: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 by EveryOS, 09 April 2018 - 05:46 PM.


#5 vyse

  • Members
  • 45 posts

Posted 09 April 2018 - 05:38 PM

View PostEveryOS, on 09 April 2018 - 05:27 PM, said:

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 by vyse, 09 April 2018 - 05:43 PM.


#6 EveryOS

  • Members
  • 570 posts
  • LocationOver there ->

Posted 09 April 2018 - 05: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 by EveryOS, 09 April 2018 - 05:47 PM.


#7 vyse

  • Members
  • 45 posts

Posted 09 April 2018 - 05:58 PM

View PostEveryOS, on 09 April 2018 - 05:47 PM, said:

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users