What is the difference between part and basepart? And what is the difference between “WhichIsA” and “OfClass”? And what do people mean by it “respects inheritance”?
A Part represents only a part instance, which those being the parts you can insert in studio.
A BasePart resembles Unions, Parts, MeshParts etc. Essentially, if you require a part, use Part. If you require an instance which has the properties of a part, use BasePart.
Essentially, Part is a pointer to a specific instance type, and BasePart is a pointer to a group of instance types.
Not the best way to explain it, but it sums it up.
A BasePart is more than just a Part
MeshParts, Parts, Terrain, WedgePart all fit in the category of BaseParts.
:IsA("") can be used to check if the instance fits that category
print(Instance.new("Part"):IsA("BasePart")) --> true
print(workspace.Terrain:IsA("BasePart")) --> true
print(Instance.new("MeshPart"):IsA("BasePart")) --> true
print(Instance.new("WedgePart"):IsA("BasePart")) --> true
This example will return the first Part, MeshPart, Terrain, Union it finds in workspace.
workspace:FindFirstChildWhichIsA("BasePart")
This however will only return the first WedgePart it finds in workspace.
It will ignore all other BasePart’s such as Meshparts or regular parts.
workspace:FindFirstChildOfClass("WedgePart")
but you could also just do workspace:FindFirstChildWhichIsA("WedgePart") right? So whats the difference?
One simple use-case is shortening if statements a bunch.
For example, if you have a character that contains a mish-mash of Unions, Meshes and Parts, and you have to change all their collision groups, you have 2 ways of writing it.
1st way, not good:
if part:IsA("Part") or part:IsA("UnionOperation") or part:IsA("MeshPart") then
2nd way, cleaner:
if part:IsA("BasePart") then
As I said, if you’re looking for a specific part type, search for the specific part type. Else, you can use BasePart.
but i still dont understand the point of part:FindFirstChildOfClass(....)
Classes are each individual Instance type, for example Model ,Player ,Part and so on, those 3 are all different classes
WeldPart, Part, MeshPart are all in the BasePart group, so they are all BaseParts, hence the IsA(“BasePart”), but what makes each one unique is their class, which, by default, is the same as their name when creating them
Imagine you had a Folder with a Part, a MeshPart, and a Part union
local Part = Folder:FindFirstChildWhichIsA("BasePart")
--The Part variable would turn into the first Part of any type that the script finds
local Part = Folder:FindFirstChildOfClass("Part")
--The Part variable will specifically search for a standard Part, ignoring mesh parts etc.
--If there is not exactly a standard Part in the folder, it will return nil
You can also do it with lights, instead of looking for point lights or spotlights or surface lights you can look for just Light i think it’s base class is and it’ll work for all 3.
Yes they are all part of the Light class