How would I check if a selected model's ClassName is in a table

I’m currently working on a plugin and I want to make it work when a player has selected a class which is allowed. For this, I’m using a table with the allowed className in it. But this is not working.

Here is what I tried so far. It might not make any sense. Howevery, any help is appreciated!

local allowedClasses = {'Model', 'Part', 'CornerWedgePart', 'MeshPart', 'TrushPart', 'WedgePart'}
local cCamera = game.Workspace.Camera:Clone()
local selectedObjects = Selection:Get()
local parent = workspace

local firstSelection = selectedObjects[1]
parent = firstSelection

if parent.ClassName == allowedClasses then
	cCamera.Parent = parent
	Selection:Set({cCamera})
	cCamera.Name = "ThumbnailCamera"
	successVisible()
	wait(7)
	mainVisible()
end

I suggest doing.
Edit: This is a more simplified way of check then adding more lines for a checking function

local Allowed = {
["Model"] = true,
["Part"] = true,
["CornerWedgePart"] = true,
["MeshPart"] = true,
["TrushPart"] = true,
}

if Allowed[parent.ClassName] then
3 Likes

An alternate solution to @IEnforce_Lawz’s response would be to create a function that checks if the ClassName is in the table using a loop.

For example:

function IsInClassTable(Class)
for num, item in pairs(allowedClasses) do
    if item.ClassName == Class then
        return true
    end
end

return false
end

--use case
if IsInClassTable(parent.ClassName) then

However, I think his solution is much more practical.

1 Like

Don’t know how performance varies, but while @IEnforce_Lawz is definitely the best, quickest, and most straightforward answer, there is also table.find(allowedClasses, parent.ClassName).

1 Like

those all either fall under the basepart class or the model class, you could just say if basepart ot model.

Indexing a table value by its key is done almost instantly with a constant runtime complexity of O(1).
table.find uses a linear search method which runs in linear time, or O(n).

Big-O notation refers to the worst-case time complexity of a function with respect to it’s input size, so if you had an array of n values, and the thing you’re searching for is at the end or the array, then the number of steps a linear search would take is proportional to the size of the array, hence a worst-case runtime complexity of O(n)

1 Like