if playername == authorizedplayers[] then -- more important stuff later? Or is there an easier way to do something with the same effect?
Check If A String Is Somewhere In A Table?
Started by popdog15, Oct 07 2013 09:25 PM
6 replies to this topic
#1
Posted 07 October 2013 - 09: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
#2
Posted 07 October 2013 - 09: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 #1
Link #2
Link #3
Link #1
Link #2
Link #3
#3
Posted 07 October 2013 - 10: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
if authorizedplayers[playername] == nil then print("Incorrect username") return end
if authorizedplayers[playername] ~= pass then print("Wrong password") return end
#4
Posted 07 October 2013 - 10:55 PM
0099, on 07 October 2013 - 10:50 PM, said:
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
if authorizedplayers[playername] == nil then print("Incorrect username") return end
if authorizedplayers[playername] ~= pass then print("Wrong password") return end
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.
#5
Posted 09 October 2013 - 04: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.
#6
Posted 09 October 2013 - 04:24 AM
Tjakka5, on 09 October 2013 - 04:19 AM, said:
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.#7
Posted 09 October 2013 - 07: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:
He's right. It would still be more efficient to do:
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
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











