Roblox "Or" Condition not working

Hello there the users of devforum. I’ve come across an error in my script where using this Or condition doesn’t work. Though it works I have a bunch of other tools in my game that can somehow open this even when its not in this code. Ideas?

if hit.Parent.Name == "Keycard" or "LI Keycard" or "LV Keycard" or "LX Keycard" or "LO Keycard" then

When you use Or you need to specify the conditions your trying to compare against, it wont try to compare the first condition to the rest of the or statements.

if hit.Parent.Name == “Keycard” or hit.Parent.Name == “LI Keycard” or hit.Parent.Name== “LV Keycard” or hit.Parent.Name == “LX Keycard” or hit.Parent.Name== “LO Keycard” then

4 Likes

this is kinda inefficient
here is a better way to do this

local acceptableOpeners = {"Keycard", "L1 Keycard", "LV Keycard", "LX Keycard", "LO Keycard"}
if hit.Parent.Name == table.find(acceptableOpeners, acceptableOpeners[hit.Parent.Name])
     print("mhm. object found. yay."
end
1 Like

It should be like this

local acceptableOpeners = {"Keycard", "L1 Keycard", "LV Keycard", "LX Keycard", "LO Keycard"}
if table.find(acceptableOpeners, hit.Parent.Name) then
     print("mhm. object found. yay.")
end
4 Likes

my bad, sorry,

thank u for correcting it

2 Likes

That’s also kinda inefficient. You’re using table.find with array-like tables even though you don’t need the indices. You should use table indexing with dictionary-like tables as that wouldn’t traverse the entire array to check if some string is in.

local acceptableOpeners = {
    ["Keycard"] = true,
    ["L1 Keycard"] = true,
    ["LV Keycard"] = true,
    ["LX Keycard"] = true,
    ["LO Keycard"] = true
}
if acceptableOpeners[hit.Parent.Name] then
    print("object found")
end
1 Like