How to feed multiple items into `IsA`?

How would be able to feed a table of strings into IsA without using a for-loop. I thought of using table.unpack, but that would just put it all in at once. I could create a modified version of IsA, but I don’t know how it really works. Any help would be much appreciated

What are you trying to do?

I’m trying to take an argument of a table and then feed it into IsA, but using for-loops gets messy:

function (Table: { string })
    ...
    if :IsA(Table) then -- Should look like :IsA("Item") and not :IsA({ "Items" })
        ...
    end
end

I think there you go:

function IsAnyOf(instance, tableOfClasses)
	return table.find(tableOfClasses, instance.ClassName) ~= nil
end

local instance = game.Workspace:WaitForChild("Part1")

if IsAnyOf(instance, {"Part", "Model", "Folder"}) then
	print("The instance is one of the specified types!")
else
	print("The instance is not one of the specified types. Check your types, bro")
end
1 Like

The only way to achieve that is to use for-loops. You can make a function that abstracts the process and returns a boolean:

local function instance_isA(instance: Instance, classNames: {string}): boolean
    for _, className in classNames do
        if instance:IsA(className) then
           return true
        end
    end
    return false
end

-- example usage
local part = Instance.new("Part")
local result = instance_isA(part, {"BasePart", "Part"})
print(result) --> 'true'

This will do. I will have to build on it a bit, though

That’s what I did before, but I had too many for-loops and it just didn’t flow well

This may be unreliable when trying to check for super classes, for example BasePart or ValueBase. If you want the code to act exactly as IsA you need to use that function specifically for each class passed in:

local function isAnyOf(instance: Instance, classes: {string}): boolean
	for _, class in pairs(classes) do
		if instance:IsA(class) then return true end
	end
	return false
end

Your function however is useful for a specific scenario, where we aren’t checking for super classes, we want to check for a large amount of possible classes that aren’t related through a super class, and we want it to be incredibly fast.

Yes, I absolutely agree with you, but I was just doing what was written in the post:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.