IsA (Method)

From Legacy Roblox Wiki
Revision as of 14:48, 22 April 2012 by >Anaminus (Rejected the last text change (by SoulStealer9875) and restored revision 55544 by Flurite: camelCase is a naming convention of arguments)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
IsA( string className )
Returns bool
Description: Returns true if the Instance is that class or a subclass.
Member of: Instance


Description

IsA provides a mechanism for introspection of the Roblox type hierarchy. Specifically, it is used to determine if an object has a given type anywhere in its inheritance tree. This is useful because there are several abstract classes in the type hierarchy, such as Frame, used by GUI objects, and BasePart, used by all physical parts and part-like objects. Prior to the addition of these abstract classes, a check to the className of an instance was sufficient to determine it's type, however, now IsA must be used to ensure correct behavior in all cases.

The most common example of IsA being necessary for code to function properly is in descending the object hierarchy searching for all physical parts in a map:

Example
--finds all parts in the given object, and adds them to the table `out`
--this is typically the most efficient implementation of a "find all parts"
--function, as it does not involve any temporary storage being allocated,
--and can be used to modify a table of children, as well as creating one 
function findAllParts(object, out)
    for _, child in pairs(object:GetChildren()) do
        if child:IsA("BasePart") then
          out[#out+1] = child
        end
        findAllParts(child, out)
    end
end

A more mundane example:

print( Workspace:IsA( "Instance" ) ) --> true
print( Workspace:IsA( "Workspace" ) ) --> true
print( game:IsA( "Workspace" ) ) --> false
print( game:IsA( "DataModel" ) ) --> true


See Object Hierarchy to see what other useful IsA relationships exist.