Check if something is a Vector3 or BasePart

I need to know if the parameter inputted into a function is a basepart or a vector3 value. Im pretty sure :IsA() errors for vector3s and value.X errors for baseparts. is there a way to do this without passing in extra parameters specifying the nature of the value?

3 Likes

You can do this:

if typeof(variable) == "Instance" and variable:IsA("BasePart") then
   -- checks if the variable is an Instance, then if it's a BasePart
   --...
elseif type(variable) == "vector" then -- roblox recently added the "vector" type
   -- you can also use typeof(variable) == "Vector3"
   --...
end

type
typeof

9 Likes

Theoretically, it’s obsolete to check for both the condition typeof(variable) == "Instance" and the condition variable:IsA('BasePart'). If the second condition is true, the first condition will always be true, too. This is because any object of class BasePart is always of the type Instance. A small note for the future intending on improving efficiency and reducing the amount of conditions you’re writing in your code.

This checks if the variable is an Instance first before trying to use IsA; if the variable is a Vector3, it would do Vector3:IsA("BasePart") which would lead to an error

1 Like

Good point, I didn’t think of that! My bad haha :sweat_smile: I’ve updated my post and quoted your response in it

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