Weird if / then statement conditions

I want to shorten the if / then statement once again by removing unnecessary object.Name =='s but it becomes weird

Checks every condition:

local object = script.Parent
if object.Name == "name1" or object.Name == "name2" or object.Name == "name3" then
	print("Hello World!")
end

If the object name is name1, name2 or name3 it will print Hello World!
---
Checks only the first condition:

local object = script.Parent
if object.Name == ("name1" or "name2" or "name3") then
	print("Hello World!")
end

If the object name is name1 it will print Hello World! but if the name is name2 or name3 it will do nothing
---
Checks only the second condition:

local object = script.Parent
if object.Name == ("name1" and "name2" or "name3") then
	print("Hello World!")
end

It will print if its name is name2
---
Checks only the third condition:

local object = script.Parent
if object.Name == ("name1" and "name2" and "name3") then
	print("Hello World!")
end

Now, it prints Hello World! only when the object name is name3

I guess it checks the latest and else the first or

So how do I keep them normal without repeating object.Name ==?

So you want it when an object is named name 1 2 and 3 it says hello world

If I understood you correctly -

Using Ternary might be helpful here.

Ternary[link 2] can be very helpful to shorten if statements.

1 Like

just do a for loop which loops with the names.
example:

local PossibleNames = {"name1","name2","name3"}
local ObjectName = script.Parent.Name

for _,v in pairs(PossibleNames) do
if ObjectName == v then
print("hello world")
end
end

I hope I was helpful.

1 Like
local object = script.Parent
local names = {"name1", "name2", "name3"}
if table.find(names, object.Name) then print("Hello world!") end

A table is the right idea, table.find() can be used if you want to shorten the code even further.

1 Like

Ik I’m kind of late but If you want it to have name1 only be the one to say hello world then you would have to set an elseif statement