How do I have one thing equal more than one things?
#1
Posted 29 September 2012 - 07:57 PM
I'm trying to use "usernamecorrect = { kadecamz, Kadecamz }" and "if input == usernamecorrect".
But its not working, please tell me what I'm doing wrong.
#2
Posted 29 September 2012 - 08:00 PM
usernameCorrect = "kadecamz"
input = string.lower(read("*"))
if input == usernameCorrect then
--stuff to do
end
This way you could type in kadecamz, Kadecamz or kAdEcAmZ if you want, and it would still work.
#3
Posted 29 September 2012 - 08:00 PM
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.
#4
Posted 29 September 2012 - 08:10 PM
-- 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 endand then got a array/table with different usernames
usernamecorrect = {
"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
#5
Posted 29 September 2012 - 08:18 PM
Doyle3694, on 29 September 2012 - 08:00 PM, said:
it saves usernames and passwords inside files.. I'm right now coding so that different users can do different stuff.
You just got me thinking of making a user system of my own!
#6
Posted 29 September 2012 - 08:23 PM
And now I am getting a problem with my code on line 16
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
#7
Posted 29 September 2012 - 08:23 PM
#8
Posted 29 September 2012 - 08:30 PM
#9
Posted 29 September 2012 - 08:31 PM
When i press enter
#10
Posted 29 September 2012 - 08:32 PM
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
...
#11
Posted 29 September 2012 - 08:34 PM
#12
Posted 29 September 2012 - 08:53 PM
MysticT, on 29 September 2012 - 08:32 PM, said:
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!
#13
Posted 29 September 2012 - 09:25 PM
#14
Posted 29 September 2012 - 09:35 PM
jag_e_nummer_ett, on 29 September 2012 - 08:53 PM, said:
MysticT, on 29 September 2012 - 08:32 PM, said:
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".
#15
Posted 29 September 2012 - 09:37 PM
MysticT, on 29 September 2012 - 09:35 PM, said:
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".
So then go with your version
MysticT, on 29 September 2012 - 08:32 PM, said:
local function table_contains(t, val) for _,v in ipairs(t) do if v == val then return true end end return false end
#16
Posted 29 September 2012 - 10:42 PM
sadface
Oh...I'm in 1.4.1....
sadface
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











