Instance

Show Deprecated
not creatable
not browsable

Instance is the base class for all classes in the Roblox class hierarchy. Every other class that the Roblox engine defines inherits all of the members of Instance. It is not possible to directly create Instance objects.

Instance has a special function called Instance.new() which is used to create objects via code. This function takes the name of the class as a parameter and returns the created object. Abstract classes and services cannot be created with the Instance.new function.

Summary

Properties

Methods

Events

Properties

Archivable

read parallel

This property determines whether the instance should be included when the experience is published or saved, or when Clone() is called on one of the instance's ancestors. Calling Clone() directly on an instance will return nil if that instance is not Archivable.

Copying an object in Studio using the Duplicate or Copy/Paste options will ignore its own Archivable property and set Archivable to true for the copy.


local part = Instance.new("Part")
print(part:Clone()) --> Part
part.Archivable = false
print(part:Clone()) --> nil

ClassName

read only
not replicated
read parallel

A read-only string representing the class this Instance belongs to.

This property can be used with various other functions of Instance that are used to identify objects by type, such as Instance:IsA() or Instance:FindFirstChildOfClass().

Note this property is read only and cannot be altered by scripts. Developers wishing to change an instance's class will instead have to create a new Instance.

Unlike Instance:IsA(), ClassName can be used to check if an object belongs to a specific class ignoring class inheritance. For example:


for _, child in workspace:GetChildren() do
if child.ClassName == "Part" then
print("Found a Part")
-- will find Parts in model, but NOT TrussParts, WedgeParts, etc
end
end

Name

read parallel

A non-unique identifier of the Instance.

This property is an identifier that describes an object. Names are not necessarily unique identifiers however; multiple children of an object may share the same name. Names are used to keep the object hierarchy organized, along with allowing scripts to access specific objects. The name of an instance cannot exceed 100 characters in size.

The name of an object is often used to access the object through the data model hierarchy using the following methods:


local baseplate = workspace.Baseplate
local baseplate = workspace["Baseplate"]
local baseplate = workspace:FindFirstChild("BasePlate")

In order to make an object accessible using the dot operator, an object's Name must follow a certain syntax. The objects name must start with an underscore or letter. The rest of the name can only contain letters, numbers, or underscores (no other special characters). If an objects name does not follow this syntax it will not be accessible using the dot operator and Lua will not interpret its name as an identifier.

If more than one object with the same name are siblings then any attempt to index an object by that name will return the only one of the objects found similar to Instance:FindFirstChild(), but not always the desired object. If a specific object needs to be accessed through code, it is recommended to give it a unique name, or guarantee that none of its siblings share the same name as it.

Note, a full name showing the instance's hierarchy can be obtained using Instance:GetFullName().

Parent

not replicated
read parallel

The Parent property determines the hierarchical parent of the Instance. The following terminology is commonly used when talking about how this property is set:

  • An object is a child (parented to) another object when its Parent is set to that object.
  • The descendants of an Instance are the children of that object, plus the descendants of the children as well.
  • The ancestors of an Instance are all the objects that the Instance is a descendant of.

It is from this property that many other API members get their name, such as GetChildren and FindFirstChild.

The Remove function sets this property to nil. Calling Destroy will set the Parent of an Instance and all of its descendants to nil, and also lock the Parent property. An error is raised when setting the Parent of a destroyed object.

This property is also used to manage whether an object exists in the game or needs removed. As long as an objects parent is in the DataModel, is stored in a variable, or is referenced by another objects property, then the object remains in the game. Otherwise, the object will automatically be removed. The top level DataModel object (the one referred to as the game by scripts) has no parent, but always has a reference held to it by the game engine, and exists for the duration of a session.

Newly created objects using Instance.new() will not have a parent, and usually will not be visible or function until one is set. The most elementary creation of an object has two steps: creating the object, then setting its parent.


-- Create a part and parent it to the workspace
local part = Instance.new("Part")
part.Parent = workspace
-- Instance new can also take Parent as a second parameter
Instance.new("NumberValue", workspace)

Object Replication

An object created by server will not replicate to clients until it is parented to some object that is replicated. When creating an object then setting many properties, it's recommended to set Parent last. This ensures the object replicates once, instead of replicating many property changes.


local part = Instance.new("Part") -- Avoid using the second parameter here
part.Anchored = true
part.BrickColor = BrickColor.new("Really red")
-- Potentially many other property changes could go here here...
-- Always set parent last!
part.Parent = workspace

However, if you were parenting your parts to a Model whose parent hasn't been set yet, then setting the parent first would not matter as the model would not have replicated yet.

RobloxLocked

hidden
read parallel

This property used to protect objects in the CoreGui service from being altered by users in an unauthorized manner. It has been deprecated and does not do anything.

Methods

AddTag

void

This method applies a tag to the instance, with no effect if the tag is already applied. Successfully adding a tag will fire a signal created by CollectionService:GetInstanceAddedSignal() with the given tag.

Note that when tagging an instance, it's common that some resources are used to give the tag its functionality, for example event connections or tables. To prevent memory leaks, it's a good idea to clean these up (disconnect, set to nil, etc.) when no longer needed for a tag. Do this when calling Instance:RemoveTag(), calling Instance:Destroy(), or in a function connected to a signal returned by CollectionService:GetInstanceRemovedSignal().

Parameters

tag: string

Returns

void

ClearAllChildren

void

This function destroys all of an instance's children.

As Instance:Destroy() also calls itself on the children of an object it is used on, this function will destroy all descendants.

Alternatives to ClearAllChildren

If the developer does not wish to destroy all descendants, they should use Instance:GetChildren() or Instance:GetDescendants() to loop through an object and select what to destroy. For example, the following code sample will destroy all parts in an object.


for _, instance in object:GetDescendants() do
if instance:IsA("BasePart") then
instance:Destroy()
end
end

Returns

void

Code Samples

Instance:ClearAllChildren

local part = Instance.new("Part")
-- add some sparkles
for _ = 1, 3 do
local sparkles = Instance.new("Sparkles")
sparkles.Parent = part
end
print("Part has", #part:GetChildren(), "children")
--> Part has 3 children
part:ClearAllChildren()
print("Part has", #part:GetChildren(), "children")
--> Part has 0 children

Clone creates a copy of an instance and all of its descendants, ignoring all instances that are not Archivable. The copy of the root instance is returned by this method and its Parent is set to nil. Note that if the instance itself has Archivable set to false, this function will return nil.

If a reference property such as ObjectValue.Value is set in a cloned instance, the value of the copy's property depends on original's value:

  • If a reference property refers to an instance that was also cloned, the copy will refer to the copy.
  • If a reference property refers to an object that was not cloned, the same value is maintained in the copy.

Returns

Code Samples

Cloning an Instance

local Workspace = game:GetService("Workspace")
-- Get a reference to an existing object
local model = script.Parent.Model
-- Create a clone of the model
local clone = model:Clone()
-- Move the clone so it's not overlapping the original model
clone:PivotTo(model.PrimaryPart.CFrame - (Vector3.xAxis * 10))
-- Add the clone to the Workspace
clone.Parent = Workspace

Destroy

void

Sets the Instance.Parent property to nil, locks the Instance.Parent property, disconnects all connections, and calls Destroy on all children. This function is the correct way to dispose of objects that are no longer required. Disposing of unneeded objects is important, since unnecessary objects and connections in a place use up memory (this is called a memory leak) which can lead to serious performance issues over time.

Tip: After calling Destroy on an object, set any variables referencing the object (or its descendants) to nil. This prevents your code from accessing anything to do with the object.


local part = Instance.new("Part")
part.Name = "Hello, world"
part:Destroy()
-- Don't do this:
print(part.Name) --> "Hello, world"
-- Do this to prevent the above line from working:
part = nil

Once an Instance has been destroyed by this method it cannot be reused because the Instance.Parent property is locked. To temporarily remove an object, set Parent it to nil instead. For example:


object.Parent = nil
wait(2)
object.Parent = workspace

To Destroy an object after a set amount of time, use Debris:AddItem().


Returns

void

Code Samples

Instance:Destroy()

local part = script.Parent.Part
part:Destroy()

FindFirstAncestor

write parallel

Returns the first ancestor of the Instance whose Instance.Name is equal to the given name.

This function works upwards, meaning it starts at the instance's immediate Instance.Parent and works up towards the DataModel. If no matching ancestor is found, it returns nil.

The following code snippet would find the first ancestor of the object named 'Car'.


local car = object:FindFirstAncestor("Car")

For variants of this function that find ancestors of a specific class, please see Instance:FindFirstAncestorOfClass() and Instance:FindFirstAncestorWhichIsA().

Parameters

name: string

The Instance.Name to be looked for.


Returns

The Instance found.

FindFirstAncestorOfClass

write parallel

Returns the first ancestor of the Instance whose Instance.ClassName is equal to the given className.

This function works upwards, meaning it starts at the instance's immediate Instance.Parent and works up towards the DataModel. If no matching ancestor is found, it returns nil.

A common use of this function is finding the Model a BasePart belongs to. For example:


local model = part:FindFirstAncestorOfClass("Model")

This function is a variant of Instance:FindFirstAncestor() which checks the Instance.ClassName property rather than Instance.Name. Instance:FindFirstAncestorWhichIsA() also exists, using the Instance:IsA() method instead to respect class inheritance.

Parameters

className: string

The Instance.ClassName to be looked for.


Returns

The Instance found.

FindFirstAncestorWhichIsA

write parallel

Returns the first ancestor of the Instance for whom Instance:IsA() returns true for the given className.

This function works upwards, meaning it starts at the instance's immediate Instance.Parent and works up towards the DataModel. If no matching ancestor is found, it returns nil.

Unlike Instance:FindFirstAncestorOfClass(), this function uses Instance:IsA() which respects class inheritance. For example:


print(part:IsA("Part")) --> true
print(part:IsA("BasePart")) --> true
print(part:IsA("Instance")) --> true

Therefore, the following code sample will return the first BasePart ancestor, regardless of if it is a WedgePart, MeshPart or Part.


local part = object:FindFirstAncestorWhichIsA("BasePart")

See also, Instance:FindFirstAncestor().

Parameters

className: string

The Instance.ClassName to be looked for.


Returns

The Instance found.

FindFirstChild

write parallel

Returns the first child of the Instance with the given name,

or nil if no such child exists. If the optional recursive argument is true, this function searches all descendants rather than only the immediate children of the Instance.

Checking the Existence of an Object

FindFirstChild is necessary if you need to verify an object exists before continuing. Attempting to index a child by name using the dot operator throws an error if the child doesn't exist.


-- The following line errors if Part doesn't exist in the Workspace:
workspace.Part.Transparency = 0.5

Use FindFirstChild to first check for Part, then use an if-statement to run code that needs it.


local part = workspace:FindFirstChild("Part")
if part then
part.Transparency = 0.5
end

Finding a Child Whose Name Matches a Property

Sometimes the Name of an object is the same as that of a property of its Parent. When using the dot operator, properties take precedence over children if they share a name.

In the following example, a Folder called "Color" is added to a Part, which also has the Part.Color property. Part.Color refers to the Color3, not the Folder.


local part = Instance.new("Part")
local folder = Instance.new("Folder")
folder.Name = "Color"
folder.Parent = part
local c = part.Color --> A Color3
local c2 = part:FindFirstChild("Color") --> The Folder

A benefit of using FindFirstChild() in this way is that the introduction of new properties does not impose a risk on your code.

Performance Note

FindFirstChild() takes about 20% longer than using the dot operator and almost 8 times longer than simply storing a reference to an object. Therefore, you should avoid calling it in performance-dependent code such as in tight loops or functions connected to RunService.Heartbeat and RunService.PreRender. Instead, store the result in a variable, or consider using ChildAdded or WaitForChild() to detect when a child of a given name becomes available.

Parameters

name: string

The Instance.Name to be searched for.

recursive: bool

Whether or not the search should be conducted recursively.

Default Value: false

Returns

The Instance found.

Code Samples

Instance:FindFirstChild

local found = workspace:FindFirstChild("Brick")
if found then
found.Name = "Foo"
end

FindFirstChildOfClass

write parallel

Returns the first child of the Instance whose ClassName is equal to the given className.

If no matching child is found, this function returns nil.

Unlike Instance:FindFirstChildWhichIsA() this function uses only returns objects whose class matches the given className, ignoring class inheritance.

Developers looking for a child by name should use Instance:FindFirstChild() instead.

Parameters

className: string

The Instance.ClassName to be looked for.


Returns

The Instance found.

Code Samples

Instance:FindFirstChildOfClass

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid
while not humanoid do
humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
character.ChildAdded:Wait()
end
end

FindFirstChildWhichIsA

write parallel

Returns the first child of the Instance for whom Instance:IsA() returns true for the given className.

If no matching child is found, this function returns nil. If the optional recursive argument is true, this function searches all descendants rather than only the immediate children of the Instance.

Unlike Instance:FindFirstChildOfClass(), this function uses Instance:IsA() which respects class inheritance. For example:


print(part:IsA("Part")) --> true
print(part:IsA("BasePart")) --> true
print(part:IsA("Instance")) --> true

Therefore, the following code sample will return the first BasePart child, regardless of if it is a WedgePart, MeshPart or Part.


local part = object:FindFirstChildWhichIsA("BasePart")

Developers looking for a child by name, should use Instance:FindFirstChild() instead.

Parameters

className: string

The Instance.ClassName to be searched for.

recursive: bool

Whether or not the search should be conducted recursively.

Default Value: false

Returns

The Instance found.

FindFirstDescendant

write parallel

Returns the first descendant found with the given Instance.Name.

This method is disabled and cannot be used. To find the first descendant of an instance, consider using the recursive parameter on Instance:FindFirstChild() instead.

Parameters

name: string

The Instance.Name to search for.


Returns

The Instance found.

GetActor

write parallel

If the Instance is an Actor, the Actor itself is returned. Otherwise, its closest ancestor Actor is returned. If no ancestor is an Actor, the result is nil.


Returns

The Actor found.

GetAttribute

Variant
write parallel

This method returns the value which has been assigned to the given attribute name. If no attribute has been assigned, nil is returned.

For example, the following code snippet sets and then gets the value of the instance's InitialPosition attribute:


local part = workspace.Part
part:SetAttribute("InitialPosition", part.Position)
local initialPosition = instance:GetAttribute("InitialPosition")
print(initialPosition)

See Also

Parameters

attribute: string

The name of the attribute being retrieved.


Returns

Variant

The value which has been assigned to the given attribute name. If no attribute has been assigned, nil is returned.

GetAttributeChangedSignal

This function returns an event that behaves exactly like the Changed event, except that it only fires when the specific given attribute changes; effectively it is similar to GetPropertyChangedSignal() but for attributes.

It's generally a good idea to use this method instead of a connection to Changed with a function that checks the attribute name. Subsequent calls to this method on the same object with the same attribute name return the same event.

The following code example returns a signal that fires the function attributeChanged() when the part's InitialPosition attribute changes:


local part = workspace.Part
part:SetAttribute("InitialPosition", part.Position)
local function attributeChanged()
print("Attribute changed")
end
part:GetAttributeChangedSignal("InitialPosition"):Connect(attributeChanged)

See also Instance.AttributeChanged which fires whenever any attribute is changed on the instance.

Parameters

attribute: string

The name of the specified attribute for which the change signal is being returned.


Returns

An event that fires when the given attribute changes.

GetAttributes

write parallel

This method returns a dictionary of key‑value pairs for each attribute where the key is the attribute's name and the value is a non‑nil value.

For example, the following code snippet outputs an instance's attributes and values:


local part = workspace.Part
part:SetAttribute("InitialPosition", part.Position)
part:SetAttribute("CanUse", true)
for name, value in part:GetAttributes() do
print(name .. " = " .. value)
end

See also Instance:GetAttribute() which returns the value that has been assigned to the given attribute name.


Returns

A dictionary of string → variant pairs for each attribute where the string is the name of the attribute and the variant is a non-nil value.

GetChildren

write parallel

Returns an array (a numerically indexed table) containing all of the instance's direct children, or every Instance whose Parent is equal to the object. The array can be iterated upon using either a numeric or generic for-loop:


-- Numeric for-loop example
local children = workspace:GetChildren()
for i = 1, #children do
local child = children[i]
print(child.Name .. " is child number " .. i)
end

-- Generic for-loop example
local children = workspace:GetChildren()
for i, child in children do
print(child.Name .. " is child number " .. i)
end

The children are sorted by the order in which their Parent property was set to the object.

See also the GetDescendants function.


Returns

An array containing the instance's children.

Code Samples

Instance:GetChildren

local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
end

GetDebugId

not browsable

Returns a coded string of the debug ID used internally by Roblox.

Note:

  • This item is protected. Attempting to use it in a Script or LocalScript will cause an error
  • A debug ID is an ID used in debugging processes. It allows a debugger to read each instruction before an application processes it. All objects in Roblox act like processes and each run instructions (or 'code') that can be debugged if needed
  • This can be helpful for plugins which need to distinguish similar objects from one-another (such as objects that share the same name)

Parameters

scopeLength: number

The scope length.

Default Value: 4

Returns

The Debug ID string.

Code Samples

Instance:GetDebugId

print(workspace:GetDebugId()) --> 39FA_12
print(workspace:GetDebugId(10)) --> 39FA2FEF4D_12
print(workspace:GetDebugId(math.huge)) --> 12

GetDescendants

write parallel

The GetDescendants function of an object returns an array that contains all of the descendants of that object. Unlike Instance:GetChildren(), which only returns the immediate children of an object, GetDescendants will find every child of the object, every child of those children, and so on.

The arrays returned by GetDescendants are arranged so that parents come earlier than their children. Refer to the following example of a Model in the Workspace:

Workspace Descendants

Inside this model are three parts (C, D, and E) and another model (InnerModel). Inside the inner model are two more parts (A and B). Calling GetDescendants on the first model and printing the contents of the returned array would print the first level of children (InnerModel, C, D, and E) before A and B.


local descendants = workspace.Model:GetDescendants()
-- Loop through all of the descendants of the model and
-- print out their name
for index, descendant in descendants do
print(descendant.Name)
end
-- Prints:
-- C
-- D
-- E
-- InnerModel
-- A
-- B

Returns

An array containing the instance's descendants.

Code Samples

Instance:GetDescendants

local descendants = workspace:GetDescendants()
-- Loop through all of the descendants of the Workspace. If a
-- BasePart is found, the code changes that parts color to green
for _, descendant in pairs(descendants) do
if descendant:IsA("BasePart") then
descendant.BrickColor = BrickColor.Green()
end
end

GetFullName

write parallel

Returns a string describing the instance's ancestry. The string is a concatenation of the Name of the object and its ancestors, separated by periods. The DataModel (game) is not considered. For example, a Part in the Workspace may return Workspace.Part.

When called on an Instance that is not a descendant of the DataModel, this function considers all ancestors up to and including the topmost one without a Parent.

This function is useful for logging and debugging. You shouldn't attempt to parse the returned string for any useful operation; this function does not escape periods (or any other symbol) in object names. In other words, although its output often appears to be a valid Lua identifier, it is not guaranteed.


Returns

The full name of the Instance.

Code Samples

Instance:GetFullName

-- Create a simple hierarchy
local model = Instance.new("Model")
local part = Instance.new("Part")
part.Parent = model
local fire = Instance.new("Fire")
fire.Parent = part
print(fire:GetFullName()) --> Model.Part.Fire
model.Parent = workspace
print(fire:GetFullName()) --> Workspace.Model.Part.Fire
part.Name = "Hello, world"
print(fire:GetFullName()) --> Workspace.Model.Hello, world.Fire
Instance:GetFullName Lua Implementation

local function getFullName(object)
local result = object.Name
object = object.Parent
while object and object ~= game do
-- Prepend parent name
result = object.Name .. "." .. result
-- Go up the hierarchy
object = object.Parent
end
return result
end
print(getFullName(workspace.Camera)) --> Workspace.Camera

GetPropertyChangedSignal

This method returns an event that behaves exactly like the Changed event, except that it only fires when the given property changes. It's generally a good idea to use this method instead of a connection to Changed with a function that checks the property name. Subsequent calls to this method on the same object with the same property name return the same event.

ValueBase objects, such as IntValue and StringValue, use a modified Changed event that fires with the contents of their Value property. As such, this method provides a way to detect changes in other properties of those objects.

Note that this event will not pass any arguments to a connected function, so the value of the changed property must be read directly within a script.

Limitations

The event returned by this method does not fire for physics-related changes, such as when the CFrame, AssemblyLinearVelocity, AssemblyAngularVelocity, Position, or Orientation properties of a BasePart change due to gravity. To detect changes in these properties, consider using a physics-based event like RunService.PreSimulation.

Additionally, the returned event may not fire on every modification of properties that change very frequently, and/or it may not fire for such properties at all. It's recommended that you carefully test for property changes that impact game logic.

Parameters

property: string

The property to connect to.


Returns

A signal that fires whenever the property changes.

Code Samples

Old-to-New Values with Changed

local part = Instance.new("Part")
local currentColor = part.BrickColor
local function onBrickColorChanged()
local newColor = part.BrickColor
print("Color changed from", currentColor.Name, "to", newColor.Name)
currentColor = newColor
end
part:GetPropertyChangedSignal("BrickColor"):Connect(onBrickColorChanged)
part.BrickColor = BrickColor.new("Really red")
part.BrickColor = BrickColor.new("Really blue")
Changed and GetPropertyChangedSignal

local part = Instance.new("Part")
local function onBrickColorChanged()
print("My color is now " .. part.BrickColor.Name)
end
local function onChanged(property)
if property == "BrickColor" then
onBrickColorChanged()
end
end
part:GetPropertyChangedSignal("BrickColor"):Connect(onBrickColorChanged)
part.Changed:Connect(onChanged)
-- Trigger some changes (because we connected twice,
-- both of these will cause two calls to onBrickColorChanged)
part.BrickColor = BrickColor.new("Really red")
part.BrickColor = BrickColor.new("Institutional white")

GetTags

write parallel

This method returns an array of the tags applied to the given instance, as strings. You can add tags either in Studio in the Properties window or at runtime with AddTag().

This method is useful when you want to do something with multiple tags on an instance at once. However, it is inefficient to use this method to check for the existence of a single tag; instead, use HasTag() to check for a specific tag.


Returns

HasTag

write parallel

This method returns true if the provided tag has been added to the object. You can add tags either in Studio in the Properties window or at runtime with AddTag().

Parameters

tag: string

Returns

IsA

write parallel

IsA returns true if the instance's class is equivalent to or a subclass of a given class. This function is similar to the instanceof operators in other languages, and is a form of type introspection. To ignore class inheritance, test the ClassName property directly instead. For checking native Lua data types (number, string, etc) use the functions type and typeof.

Most commonly, this function is used to test if an object is some kind of part, such as Part or WedgePart, which inherits from BasePart (an abstract class). For example, if your goal is to change all of a character's limbs to the same color, you might use GetChildren to iterate over the children, then use IsA to filter non-BasePart objects which lack the BrickColor property:


local function paintFigure(character, color)
-- Iterate over the child objects of the character
for _, child in character:GetChildren() do
-- Filter out non-part objects, such as Shirt, Pants and Humanoid
-- R15 use MeshPart and R6 use Part, so we use BasePart here to detect both:
if child:IsA("BasePart") then
child.BrickColor = color
end
end
end
paintFigure(game.Players.Player.Character, BrickColor.new("Bright blue"))

Since all classes inherit from Instance, calling object:IsA("Instance") will always return true.

Parameters

className: string

The class against which the Instance's class will be checked. Case-sensitive.


Returns

Describes whether the Instance's class matched or is a subclass of the given class.

Code Samples

Instance:IsA()

local Workspace = game:GetService("Workspace")
print(Workspace:IsA("Workspace")) -- true
print(Workspace:IsA("BasePart")) -- false
print(Workspace:IsA("Instance")) -- true

IsAncestorOf

write parallel

Returns true if an Instance is an ancestor of the given descendant.

An Instance is considered the ancestor of an object if the object's Instance.Parent or one of it's parent's Instance.Parent is set to the Instance.

See also, Instance:IsDescendantOf().

Parameters

descendant: Instance

The descendant Instance.


Returns

True if the Instance is an ancestor of the given descendant.

Code Samples

Instance:IsAncestorOf()

local Workspace = game:GetService("Workspace")
local spawnLocation = Workspace.SpawnLocation
local decal = spawnLocation.Decal
-- These statements are true
print(Workspace:IsAncestorOf(spawnLocation))
print(Workspace:IsAncestorOf(decal))
print(spawnLocation:IsAncestorOf(decal))
-- These statements are false
print(spawnLocation:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(spawnLocation))

IsDescendantOf

write parallel

Returns true if an Instance is a descendant of the given ancestor.

An Instance is considered the descendant of an object if the instance's parent or one of its parent's parent is set to the object.

Note, DataModel is a descendant of nil. This means IsDescendantOf cannot be used with a parameter of nil to check if an object has been removed.

See also, Instance:IsAncestorOf().

Parameters

ancestor: Instance

The ancestor Instance.


Returns

True if the Instance is a descendant of the given ancestor.

Code Samples

Instance:IsDescendantOf

local part = Instance.new("Part")
print(part:IsDescendantOf(game))
--> false
part.Parent = workspace
print(part:IsDescendantOf(game))
--> true
part.Parent = game
print(part:IsDescendantOf(game))
--> true

RemoveTag

void

This method removes a tag from an instance. It will not throw an error if the object does not have the tag. Successfully removing a tag will fire a signal created by CollectionService:GetInstanceRemovedSignal() with the given tag.

Note that when tagging an instance, it's common that some resources are used to give the tag its functionality, for example event connections or tables. To prevent memory leaks, it's a good idea to clean these up (disconnect, set to nil, etc.) when no longer needed for a tag.

Parameters

tag: string

Returns

void

SetAttribute

void

This method sets the attribute with the given name to the given value. If the value given is nil, the attribute will be removed, since nil is returned by default.

For example, the following code snippet sets the instance's InitialPosition attribute to Vector3.new(0, 10, 0):


local part = workspace.Part
part:SetAttribute("InitialPosition", Vector3.new(0, 10, 0))

Limitations

Naming requirements and restrictions:

  • Names must only use alphanumeric characters and underscore.
  • No spaces or unique symbols are allowed.
  • Strings must be 100 characters or less.
  • Names are not allowed to start with RBX unless the caller is a Roblox core script (reserved for Roblox).

When attempting to set an attribute to an unsupported type, an error will be thrown.

See also:

Parameters

attribute: string

The name of the attribute being set.

value: Variant

The value to set the specified attribute to.


Returns

void

WaitForChild

can yield

Returns the child of the Instance with the given name. If the child does not exist, it will yield the current thread until it does. If the timeOut parameter is specified, this method will time out after the specified number of seconds and return nil.

Primary Usage

WaitForChild() is extremely important when working on code run by the client in a LocalScript. The Roblox engine does not guarantee the time or order in which objects are replicated from the server to the client. Additionally, if an experience has Workspace.StreamingEnabled set to true, BaseParts that are far away from the player's character may not be streamed to the client, potentially causing scripts to break when indexing objects that do not yet exist on the client.

Notes

  • This function does not yield if a child with the given name exists when the call is made.
  • Instance:FindFirstChild() is a more efficient alternative to WaitForChild() for objects that are assumed to exist.
  • If a call to this method exceeds 5 seconds without returning, and no timeOut parameter has been specified, a warning will be printed to the output that the thread may yield indefinitely.

Parameters

childName: string

The Instance.Name to be looked for.

timeOut: number

An optional time out parameter.


Returns

The Instance found.

Code Samples

Instance:WaitForChild

local part = workspace:WaitForChild("Part")
print(part.Name .. " has been added to the Workspace")

Events

AncestryChanged

Fires when the Instance.Parent property of the object or one of its ancestors is changed.

This event includes two parameters, child and parent. Child refers to the Instance whose Instance.Parent was actually changed. Parent refers to this instance's new Instance.Parent.

You can use this event to track the deletion of an instance in Studio, such as manual deletion in the Explorer or through a plugin. If you need to detect when an instance is destroyed using Instance:Destroy(), use the Instance.Destroying event instead.

Parameters

child: Instance

The Instance whose Instance.Parent has been changed.

parent: Instance

The new Instance.Parent of the Instance whose Instance.Parent was changed.


Code Samples

Instance.AncestryChanged

local Workspace = game:GetService("Workspace")
local redPart = script.Parent.RedPart
local bluePart = script.Parent.BluePart
local changingPart = script.Parent.ChangingPart
-- Change the color of changingPart based on it's Parent
local function onAncestryChanged(part: Part, parent: Instance)
if parent == redPart then
changingPart.Color = Color3.new(1, 0, 0)
elseif parent == bluePart then
changingPart.Color = Color3.new(0, 0, 1)
else
changingPart.Color = Color3.new(1, 1, 1)
end
print(`{part.Name} is now parented to {parent.Name}`)
end
changingPart.AncestryChanged:Connect(onAncestryChanged)
-- Set changingPart's Parent property to different instances over time
while true do
task.wait(2)
changingPart.Parent = redPart
task.wait(2)
changingPart.Parent = bluePart
task.wait(2)
changingPart.Parent = Workspace
end

AttributeChanged

This event fires whenever any attribute is changed on the instance, including when an attribute is set to nil. The name of the changed attribute is passed to the connected function.

For example, the following code snippet connects the attributeChanged() function to fire whenever one of instance's attributes changes:


local part = workspace.Part
local function attributeChanged(attributeName)
print(attributeName, "changed")
end
part.AttributeChanged:Connect(attributeChanged)

See also Instance:GetAttributeChangedSignal() which returns an event that fires when a specific given attribute changes.

Parameters

attribute: string

The name of the attribute that has been changed.


Changed

This event fires immediately after most properties change on the instance. It is possible to find the present value of a changed property by using object[property].

If you are only interested in listening to the change of one specific property, consider using the GetPropertyChangedSignal() method instead.

For ValueBase objects such as IntValue and StringValue, this event only fires when the object's Value property changes. To detect other changes in ValueBase objects, use GetPropertyChangedSignal() instead.

Limitations

This event does not fire for physics-related changes, such as when the CFrame, AssemblyLinearVelocity, AssemblyAngularVelocity, Position, or Orientation properties of a BasePart change due to gravity. To detect changes in these properties, consider using a physics-based event like RunService.PreSimulation.

Additionally, this event may not fire on every modification of properties that change very frequently, and/or it may not fire for such properties at all. It's recommended that you carefully test for property changes that impact game logic.

Parameters

property: string

The name of the property that changed.


Code Samples

Changed Event

-- Demonstrate the Changed event by creating a Part
local part = Instance.new("Part")
part.Changed:Connect(print)
-- This fires Changed with "Transparency"
part.Transparency = 0.5
-- Similarly, this fires Changed with "Number"
part.Name = "SomePart"
-- Since changing BrickColor will also change other
-- properties at the same time, this line fires Changed
-- with "BrickColor", "Color3" and "Color3uint16".
part.BrickColor = BrickColor.Red()
-- A NumberValue holds a double-precision floating-point number
local vNumber = Instance.new("NumberValue")
vNumber.Changed:Connect(print)
-- This fires Changed with 123.456 (not "Value")
vNumber.Value = 123.456
-- This does not fire Changed
vNumber.Name = "SomeNumber"
-- A StringValue stores one string
local vString = Instance.new("StringValue")
vString.Changed:Connect(print)
-- This fires Changed with "Hello" (not "Value")
vString.Value = "Hello"
Change Detector

local object = script.Parent
local function onChanged(property)
-- Get the current value of the property
local value = object[property]
-- Print a message saying what changed
print(object:GetFullName() .. "." .. property .. " (" .. typeof(value) .. ") changed to " .. tostring(value))
end
object.Changed:Connect(onChanged)
-- Trigger a simple change in the object (add an underscore to the name)
object.Name = "_" .. object.Name

ChildAdded

Fires after an object is parented to this Instance.

Note, when using this function on a client to detect objects created by the server it is necessary to use Instance:WaitForChild() when indexing these object's descendants. This is because the object and its descendants are not guaranteed to replicate from the server to the client simultaneously. For example:


workspace.ChildAdded:Connect(function(child)
-- need to use WaitForChild as descendants may not have replicated yet
local head = child:WaitForChild("Head")
end)

Note, this function only works for immediate children of the Instance. For a function that captures all descendants, use Instance.DescendantAdded.

See also, Instance.ChildRemoved.

Parameters

child: Instance

The Instance that has been added.


Code Samples

Instance.ChildAdded

local function onChildAdded(instance)
print(instance.Name .. " added to the workspace")
end
workspace.ChildAdded:Connect(onChildAdded)
local part = Instance.new("Part")
part.Parent = workspace --> Part added to the Workspace

ChildRemoved

Fires after a child is removed from this Instance.

Removed refers to when an object's parent is changed from this Instance to something other than this Instance. Note, this event will also fire when a child is destroyed (using Instance:Destroy()) as the destroy function sets an object's parent to nil.

This function only works for immediate children of the Instance. For a function that captures all descendants, use Instance.DescendantRemoving.

See also Instance.ChildAdded.

Parameters

child: Instance

The Instance that has been removed.


Code Samples

Instance.ChildRemoved

local function onChildRemoved(instance)
print(instance.Name .. " removed from the workspace")
end
workspace.ChildRemoved:Connect(onChildRemoved)
local part = Instance.new("Part")
part.Parent = workspace
task.wait(2)
part:Destroy()

DescendantAdded

The DescendantAdded event fires after a descendant is added to the Instance.

As DescendantAdded fires for every descendant, parenting an object to the Instance will fire the event for this object and all of its descendants individually.

Developers only concerned with the immediate children of the Instance should use Instance.ChildAdded instead.

See also Instance.DescendantRemoving.

Parameters

descendant: Instance

The Instance that has been added.


Code Samples

Instance.DescendantAdded

local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspace

DescendantRemoving

This event fires immediately before the parent Instance changes such that a descendant instance will no longer be a descendant. Destroy() changes an instance's Parent to nil, so calling that method on a descendant of the parent will cause this event to fire.

Since this event fires before the descendant's removal, the parent of the descendant will be unchanged at the time of this event firing. If the descendant is also a direct child of the parent, this event will fire before Instance.ChildRemoved.

If a descendant has children, this event fires with the descendant first, followed by its descendants.

Warning

This event fires with the descendant object that is being removed. Attempting to set the Parent of the descendant to something else will fail. Below is an example that demonstrates this:


workspace.DescendantRemoving:Connect(function(descendant)
-- Do not manipulate the parent of the descendant in this function!
-- This event fires BECAUSE the parent was manipulated, and the change hasn't happened yet
-- Therefore, it is problematic to change the parent like this:
descendant.Parent = game
end)
local part = Instance.new("Part")
part.Parent = workspace
part.Parent = nil

See also DescendantAdded.

Parameters

descendant: Instance

The Instance that is being removed.


Code Samples

Instance.DescendantRemoving

workspace.DescendantRemoving:Connect(function(descendant)
print(descendant.Name .. " is currently parented to " .. tostring(descendant.Parent))
end)
local part = Instance.new("Part")
part.Parent = workspace
part.Parent = nil
--> Part is currently parented to Workspace
print(part.Parent)
--> nil

Destroying

The Destroying event fires immediately before the Instance or one of its ancestors is destroyed with Instance.Destroy().

The Instance will never be deleted from memory while a connected function is still using it. However, if the function yields at any point, the Instance and its descendants will be parented to nil.

When deleting an instance in Studio, such as manually deleting through the Explorer or through a plugin, the instance isn't destroyed. Instead, the parent is set to nil which you can track with Instance.AncestryChanged.


Code Samples

Using the Destroying Event

local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("Before yielding:", part:GetFullName(), #part:GetChildren())
task.wait()
print("After yielding:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
part:Destroy()