What's the difference between checking if an Instance is something in `typeof()` and `:IsA()`

Oh yeah, so it does! (I thought it would return ModuleScript, silly me) Thank you!

1 Like

That’s amazing how typeof is misunderstood…

typeof returns the datatype of the variable you pass. You can check if a variable stores a number, a string, a boolean, a table, custom ROBLOX userdata, and an instance.
Roblox implemented typeof in favor of type to handle more datatypes. I think typeof handles Instances, vectors, udims, … whereas type would return “userdata” for all of those.

The method IsA returns a boolean describing whether or not the implicit instance is of the precised class, or if the instance’s class derives from the precised class. It is only usable if typeof(var) == "Instance", aka only if the variable you manipulate IS an instance.

Example:

Case not instance

local a = 2

print(typeof(a) == "number") --true
print(a:IsA("Part")) --error, a stores a number, not a Roblox Instance !
print(a.ClassName == "Part") --error, same as above

Case part with Part check

local a = Instance.new("Part")

print(typeof(a) == "Instance") --true
print(a:IsA("Part")) --true
print(a.ClassName == "Part") --true

Case part with BasePart check

local a = Instance.new("Part")

print(typeof(a) == "Instance") --true
print(a:IsA("BasePart")) --true, Part inherits from BasePart
print(a.ClassName == "BasePart") --false, class Part not strictly equal to class BasePart. You will never find an instance which can satisfy this condition as BasePart is abstract

So if you have a variable that can hold a lot of different DATATYPES, and you want to check if it’s a BasePart, you’d do

if typeof(a) == "Instance" --check if a is an instance
  and a:IsA("BasePart") --check if a's class derives from BasePart (or is of class BasePart, as seen, it's impossible)
then
1 Like

You should really read the conversation before replying, but thank you for the information I already know anyway. :slight_smile:

I read it, and it did feel wrong. typeof and IsA don’t serve the same purpose. One isn’t better than the other, because you can’t compare then in terms of use cases !