97 posts
Location
Ohio, USA
Posted 07 June 2013 - 09:10 PM
I'm working on a somewhat-large script that sends information into a database, etc. but I am making a login system involving multiple users, so would be code be useable??
users = {"user1","user2"}
if input ~= users then
print("Your not allowed to do this")
end
1522 posts
Location
The Netherlands
Posted 07 June 2013 - 09:16 PM
You would need to use a pairs loop:
local t = {
'hi!',
'hey!'
}
for key, value in pairs(t) do
print(key..':'..value)
end
--> 1:hi
--> 2:hey!
so a function would be:
local checkFor = function(tablename, user)
for k, v in pairs(tablename) do
if v == user then
return true
end
end
return false
end
7508 posts
Location
Australia
Posted 07 June 2013 - 10:49 PM
Or alternatively another option is to define your table like this
local users = {
["user1"] = true,
["some user"] = true,
["theoriginalbit"] = true,
--# etc, etc, etc.
}
Then it would allow you to do this
if users[ input ] then
print "success"
else
print "failed"
end
Much cleaner code, and no need for `for` loops. Meaning that it runs more efficiently and there is no chance for a yield error if the table is very large.
97 posts
Location
Ohio, USA
Posted 08 June 2013 - 04:18 PM
Why can't life be simple. Thanks guys for you're help. :)/>