Checking if a variable is a part and an attachment?

Okay so, im trying to check if a variable is a part or not, and another one is checking if a variable is an attachment? How would i do that? I dont have a list of what u can do with ‘typeof()’ but im pretty sure ‘part’ and ‘attachment’ is not there, so how?

local part = workspace:WaitForChild("Part")
local attachment = part:WaitForChild("Attachment")

if part:IsA("Part") then
	-- code
end

if attachment:IsA("Attachment") then
	--do code
end

if part.ClassName == "Part" then
	--do code
end

if attachment.ClassName == "Attachment" then
	--do code
end

The function IsA() and the property .ClassName can both be used to achieve this.

3 Likes

Try this:

typeof(part) == "Instance" and part:IsA("BasePart")

The typeof(part) == "Instance" confirms it’s an Instance (otherwise Instance:IsA would error), then sees if it descends from the BasePart class.

2 Likes

If the variable isn’t an Instance (such as a part, attachment, etc.), this would error, since :IsA is something that comes from Instances (same with ClassName).

1 Like

Of course, isn’t that obvious though? Just don’t use them on primitive values.

1 Like

They asked to check if a variable is a part/attachment, and variables aren’t guaranteed to be instances.

1 Like

I assume in their case they are.

1 Like

Overall, it’s just unclear if they are or not since they mentioned typeof(). The one I suggested will make sure it’s a part with no errors, while yours will if it’s not an Instance.

2 Likes