82 posts
Posted 07 October 2013 - 10:25 PM
Hello, I'm making a program that requires a player to be "authorized" and I put all the authorized players within a table. After a player hits a player detector, I need to check if that player is within the table somewhere. Would it just be something like
if playername == authorizedplayers[] then
-- more important stuff later
? Or is there an easier way to do something with the same effect?
7508 posts
Location
Australia
Posted 07 October 2013 - 10:48 PM
This is a very common question on these forums, and there are lots of answers to all of them, both from myself and others, as such, I'm not going do a big writeup here, why say something again when you can just point people to a place where you've already said it before.
Link #1Link #2Link #3
52 posts
Posted 07 October 2013 - 11:50 PM
Maybe you can create a table with player names as KEYS, and the values will be… For example, passwords. Then, you can check for this player like that:
if authorizedplayers[playername] == nil then print("Incorrect username") return end
if authorizedplayers[playername] ~= pass then print("Wrong password") return end
7508 posts
Location
Australia
Posted 07 October 2013 - 11:55 PM
Maybe you can create a table with player names as KEYS, and the values will be… For example, passwords. Then, you can check for this player like that:
if authorizedplayers[playername] == nil then print("Incorrect username") return end
if authorizedplayers[playername] ~= pass then print("Wrong password") return end
Yes that method is the best way to do it, as detailed in all 3 of the links I provided in the reply above yours.
Also, a tip, use [
code][
/code] tags around code.
Lastly your
== nil is not needed, doing
if authorizedPlayers[playername] then will evaluate to
true if there is a value,
false if it is
nil. Your second if statement can work without the first one too.
355 posts
Posted 09 October 2013 - 05:19 AM
local players = {"Tjakka5", "person1", "person2"}
local input = read
for i = 1, #players do
if input == players[i] then
--do stuff
end
end
That should also work.
7508 posts
Location
Australia
Posted 09 October 2013 - 05:24 AM
local players = {"Tjakka5", "person1", "person2"}
local input = read
for i = 1, #players do
if input == players[i] then
--do stuff
end
end
That should also work.
Actually it wouldn't, you have a bug, but if you didn't have the bug, it would be
very inefficient compared to a lookup table especially when there is a large amount of data in the table.
131 posts
Posted 09 October 2013 - 08:24 AM
lol, I love how bit's trying to keep himself from explaining everything over again. You would have to change your code to:
local players = {"Tjakka5", "person1", "person2"}
local input = read() -- forgot parentheses
for i = 1, #players do
if input == players[i] then
--do stuff
end
end
He's right. It would still be more efficient to do:
local players = {Tjakka5 = true, person1 = true, person2 = true}
if players[read()] then
--do stuff
end