PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Using IsA to Check a Part's Class

Using IsA to Check a Part's Class

Jul 03 2018, 10:35 PM PST

Problem

You want to check if an object is a brick-like object, if its a truss, part, seat, wedge, or something similar.

Solution

Use the base class of parts in the Instance/IsA|IsA method.

if Workspace.Part:IsA('BasePart') then
	print("It is a part-like object")
end

Discussion

The concept of class inheritance is powerful, and is really neat. In Roblox we have objects, but also things called structural objects. This are special types of objects that you cannot create, but are used to structure creatable parts. Inheritance is a large topic in object oriented programming so this will only be a small piece of it. All Part|parts (seats, parts, trusses) have similar properties and methods. All of them have a BasePart/Size|Size property, BasePart/Position|Position property, and many other similar members. Instead of each of them existing on their own, they inherit these attributes from the structural objects. All objects have the Instance/Name|Name, Instance/ClassName|className, and archivable properties. The objects inherit these properties from an ancestor object. All objects are children of the Instance object (don’t confuse this with the Instance table that holds things like Instance.new).

The Part object extends (or is a child of) the BasePart structural object or class. In actuality, the Part object only has one property that’s the Part/Shape|Shape property. All the other properties it has comes from its parent, the BasePart class which it extends. The BasePart class has a whole slew of different fundamental properties of all parts like BasePart/Position|Position and BasePart/Size|Size. The chain all the way up until you hit the master object, Instance in which all objects extend.

When we do Instance/IsA we check if that object is or inherits members from a class. The following all result in true.

  • Instance.new('Part'):IsA('Part') --> true
  • Instance.new('Part'):IsA('BasePart') --> true
  • Instance.new('Part'):IsA('PVInstance') --> true
  • Instance.new('Part'):IsA('Instance') --> true
Tags:
  • part
  • class
  • coding