I'm trying to use "usernamecorrect = { kadecamz, Kadecamz }" and "if input == usernamecorrect".
But its not working, please tell me what I'm doing wrong.
usernameCorrect = "kadecamz"
input = string.lower(read("*"))
if input == usernameCorrect then
--stuff to do
end
local userFile
local passwordFile
local users = {}
local passwords = {}
local passwordEntered
local userEntered
local userNum = 0
local lineRead
function readUsersAndPasswords()
fs.makeDir("passwordFolder")
userFile = fs.open("passwordFolder/users","r")
passwordFile = fs.open("passwordFolder/passwords","r")
lineRead = userFile.readLine()
repeat
table.insert(users,lineRead)
lineRead = userFile.readLine()
until lineRead == nil
lineRead = passwordFile.readLine()
repeat
table.insert(passwords,lineRead)
lineRead = passwordFile.readLine()
until lineRead == nil
passwordFile.close()
userFile.close()
end
function password()
textutils.slowPrint("Please enter your password "..users[userNum], 15)
passwordEntered = read("*")
if passwordEntered == passwords[userNum] then
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Password match.", 15)
textutils.slowPrint("Welcome "..users[userNum], 15)
elseif passwordEntered == "retry" then
username()
else
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Password missmatch. Please try again.", 15)
textutils.slowPrint("Type 'retry' to enter a new username.", 15)
password()
end
end
function username()
userNum = 0
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Welcome", 15)
sleep(1)
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Please enter your username", 15)
userEntered = read()
for i=1, #users do
if userEntered == users[i] then
userNum = i
break
end
end
if userNum == 0 then
textutils.slowPrint("Username could not be recognized. Please try again", 15)
username()
else
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("Username recognized.", 15)
sleep(1)
term.clear()
term.setCursorPos(1,1)
password()
end
end
readUsersAndPasswords()
username()
end
-- The function returns true if the element is a value inside of the table
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
and then got a array/table with different usernamesusernamecorrect = {
"kadecamz",
"Kadecamz",
"anonomit",
"Anonomit",
"Doyle3694",
"doyle3694"
}
then you can easily combine the input with the function like thisinput = read()
if table.contains("usernamecorrect", input) then
-- code to be executed
end
Really cool code! And really helpful!!I'm doing exactly such a program right atm.Spoiler
local userFile local passwordFile local users = {} local passwords = {} local passwordEntered local userEntered local userNum = 0 local lineRead function readUsersAndPasswords() fs.makeDir("passwordFolder") userFile = fs.open("passwordFolder/users","r") passwordFile = fs.open("passwordFolder/passwords","r") lineRead = userFile.readLine() repeat table.insert(users,lineRead) lineRead = userFile.readLine() until lineRead == nil lineRead = passwordFile.readLine() repeat table.insert(passwords,lineRead) lineRead = passwordFile.readLine() until lineRead == nil passwordFile.close() userFile.close() end function password() textutils.slowPrint("Please enter your password "..users[userNum], 15) passwordEntered = read("*") if passwordEntered == passwords[userNum] then term.clear() term.setCursorPos(1,1) textutils.slowPrint("Password match.", 15) textutils.slowPrint("Welcome "..users[userNum], 15) elseif passwordEntered == "retry" then username() else term.clear() term.setCursorPos(1,1) textutils.slowPrint("Password missmatch. Please try again.", 15) textutils.slowPrint("Type 'retry' to enter a new username.", 15) password() end end function username() userNum = 0 term.clear() term.setCursorPos(1,1) textutils.slowPrint("Welcome", 15) sleep(1) term.clear() term.setCursorPos(1,1) textutils.slowPrint("Please enter your username", 15) userEntered = read() for i=1, #users do if userEntered == users[i] then userNum = i break end end if userNum == 0 then textutils.slowPrint("Username could not be recognized. Please try again", 15) username() else term.clear() term.setCursorPos(1,1) textutils.slowPrint("Username recognized.", 15) sleep(1) term.clear() term.setCursorPos(1,1) password() end end readUsersAndPasswords() username() end
it saves usernames and passwords inside files.. I'm right now coding so that different users can do different stuff.
usernamecorrect = {
"kadecamz",
"Kadecamz"
}
correctpassword = {
"1337",
"kade"
}
term.clear()
term.setCursorPos(1,1)
print("User/Access") --just for looks
print("ACCESS DENIED")
term.setCursorPos(17,7)
write("USERNAME: ")
username = read()
if table.contains("usernamecorrect", username) then --Getting "startup:16: attempt to call nil" on this line
print(" ")
write("Reading Username")
textutils.slowPrint("...")
print(username.." is a correct username!")
sleep(3)
term.setCursorPos(17,7)
term.clearLine()
term.setCursorPos(17,7)
write("PASSWORD: ")
password = read()
if password == correctpassword then
write("Reading Password")
textutils.slowPrint("...")
print(password.." is a correct password!")
sleep(3)
term.clear()
term.setCursorPos(1,1)
print("User/Access") --just for looks
print("GRANTED")
sleep(3)
shell.run("granted")
else
write("Reading Input")
textutils.slowPrint("...")
print("Wrong Entry!")
sleep(3)
os.reboot()
end
end
local function table_contains(t, val)
for _,v in ipairs(t) do
if v == val then
return true
end
end
return false
end
...
if table_contains(usernamecorrect, username) then
...
Let me ask you this: Did you test it?The table.contains function doesn't exist. And you can't add it just like that, because the table api is protected. You would have to define a function (with another name, and outside the table api) that has the code jag_e_nummer_ett posted (wich he probably didn't test, since it would throw an error).
So, just add the function before using it, and change table.contains to it's name:local function table_contains(t, val) for _,v in ipairs(t) do if v == val then return true end end return false end ... if table_contains(usernamecorrect, username) then ...
Hmm… it looks like they removed the global tables protection in 1.42, that's good, it wasn't really needed.Let me ask you this: Did you test it?The table.contains function doesn't exist. And you can't add it just like that, because the table api is protected. You would have to define a function (with another name, and outside the table api) that has the code jag_e_nummer_ett posted (wich he probably didn't test, since it would throw an error).
So, just add the function before using it, and change table.contains to it's name:local function table_contains(t, val) for _,v in ipairs(t) do if v == val then return true end end return false end ... if table_contains(usernamecorrect, username) then ...
Because I did and it worked!
Well in that case, you're right!Hmm… it looks like they removed the global tables protection in 1.42, that's good, it wasn't really needed.Spoiler
Let me ask you this: Did you test it?The table.contains function doesn't exist. And you can't add it just like that, because the table api is protected. You would have to define a function (with another name, and outside the table api) that has the code jag_e_nummer_ett posted (wich he probably didn't test, since it would throw an error).
So, just add the function before using it, and change table.contains to it's name:local function table_contains(t, val) for _,v in ipairs(t) do if v == val then return true end end return false end ... if table_contains(usernamecorrect, username) then ...
Because I did and it worked!
Sorry, I haven't tested 1.42 too much.
Anyway, that's in 1.42, so if someone plays in a previous version it will throw an error saying "Attempt to write to global".
local function table_contains(t, val) for _,v in ipairs(t) do if v == val then return true end end return false end
That's why I said that you need to change the name. Making the changes I posted above should work.Oh…I'm in 1.4.1….
sadface
Oh…I'm in 1.4.1….
sadface