Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

If all table values are not equal to?

Asked by 4 years ago

Basically my script looks like this right now:

local foundpart = "x"

local parts = {
    "a",
    "b",
    "c",
    "d",
    "e",
}

for i = 1, #parts do
    if parts[i] ~= foundpart then
        print("not found")
    end
end

But this script will print "not found" 5 times, I want it to just print it once.

If I use break or return it will not check all the strings in the 'parts' variable.

0
use debounce? IcyMizu 122 — 4y

1 answer

Log in to vote
2
Answered by
Azarth 3141 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You can use a dictionary instead.

local foundpart = "x"

local parts = {
   ["a"] = true,
   ["b"] = true,
   ["c"] = true,
   ["d"] = true,
   ["e"] = true,
}

if not parts[foundpart] then 
    print ( "not found")
end

or table.find()

local foundpart = "x"

local parts = {
   "a",
   "b",
   "c",
   "d",
   "e",
}

if not table.find(parts, foundpart) then 
    print ( "not found")
end
Ad

Answer this question