CoolPoolMool
CoolPoolMool

Reputation: 1

How would i check if the value in the table is equal to the _G variable string?

How would i check the table to see if the value in the table matches the _G.User = "" variable?

_G.Name = "Bod"
------------------------------------------------------------
local CorrectUsers = {
  "Username1",
  "Username2",
  "Username3",
  "Username4",
  "Username5",
}
------------------------------------------------------------
for i,v in pairs(CorrectUsers, _G.User) do
 if table.find(CorrectUsers, _G.User) then
  print("Correct User")
 else
  print("Incorrect User")
 end
end

https://replit.com/@BloodThirstyy/SimpleMultiStepWhitelist

Upvotes: 0

Views: 226

Answers (1)

Piglet
Piglet

Reputation: 28950

for i,v in pairs(CorrectUsers, _G.User) do
 if table.find(CorrectUsers, _G.User) then

pairs only takes one argument. There is no table.find in Lua's table library.

In your snippet _G.User is undefined. _G.Name is unused.

Also usually there is no need to access globals through _G. Just write User.

To find something in a table you traverse over the table and compare each element with the value you are looking for.

So for a table like

local CorrectUsers = {
  "Bob",
  "John",
  "Susan",
  "Daniel",
  "Zoe",
}

You'd simply do

for i, v in ipairs(CorrectUsers) do
  if v == "Daniel" then
    print("found Daniel")
  end
end

Alternatively you can build a look up table:

local isCorrectUser = {
  Bob = true,
  Jane = true,
  John = true,
}

And then

if isCorrectUser.Jane then
  print("found Jane")
end

As building such look up tables can become tedious you can simply create one from your user list.

local isCorrectUser = {}
for i, v in ipairs(CorrectUsers) do
  isCorrectUser[v] = true
end

Using a lookup table becomes useful if you are checking values very frequently as you will not have to traverse over the table very time.

Upvotes: 2

Related Questions