Instance
Instance is the base class for all classes in the Roblox class hierarchy which can be part of the DataModel tree.
It is not possible to directly create root Instance objects, but the special Instance.new() constructor creates objects via code, taking the name of the class as a parameter and returning the created object.
Summary
Properties
Determines if an Instance and its descendants can be cloned using Instance:Clone(), and can be saved/published.
The set of capabilities allowed to be used for scripts inside this container.
A non-unique identifier of the Instance.
Determines the hierarchical parent of the Instance.
A deprecated property that used to protect CoreGui objects.
Turns the instance to be a sandboxed container.
A unique identifier for the instance.
Methods
Applies a tag to the instance.
This function destroys all of an instance's children.
Create a copy of an instance and all its descendants, ignoring instances that are not Archivable.
Sets the Instance.Parent property to nil, locks the Instance.Parent property, disconnects all connections, and calls Destroy() on all children.
Returns the first ancestor of the Instance whose Instance.Name is equal to the given name.
Returns the first ancestor of the Instance whose Object.ClassName is equal to the given className.
Returns the first ancestor of the Instance for whom Object:IsA() returns true for the given className.
Returns the first child of the Instance found with the given name.
Returns the first child of the Instance whose ClassName is equal to the given class name.
Returns the first child of the Instance for whom Object:IsA() returns true for the given className.
Returns the first descendant found with the given Instance.Name.
Returns the Actor associated with the Instance, if any.
Returns the value which has been assigned to the given attribute name.
Returns an event that fires when the given attribute changes.
Returns a dictionary of the instance's attributes.
Returns an array containing all of the instance's children.
Returns a coded string of the debug ID used internally by Roblox.
Returns an array containing all of the descendants of the instance.
Returns a string describing the instance's ancestry.
Returns the styled or explicitly modified value of the specified property, or else the default property value if it hasn't been styled/modified.
Gets an array of all tags applied to the instance.
Check whether the instance has a given tag.
Returns true if an Instance is an ancestor of the given descendant.
Returns true if an Instance is a descendant of the given ancestor.
Returns true if the value stored in the specified property is equal to the code-instantiated default.
Removes a tag from the instance.
Resets a property to its default value.
Sets the attribute with the given name to the given value.
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.
Events
Fires when the Instance.Parent property of the object or one of its ancestors is changed.
Fires whenever an attribute is changed on the Instance.
Fires after an object is parented to this Instance.
Fires after a child is removed from this Instance.
Fires after a descendant is added to the Instance.
Fires immediately before a descendant of the Instance is removed.
Fires immediately before (or is deferred until after) the instance is destroyed via Instance:Destroy().
Fires whenever any style property is changed on the instance, including when a property is set to nil.
Properties
Methods
ClearAllChildren
Returns
Clone
Returns
Code Samples
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
Returns
Code Samples
local part = script.Parent.Part
part:Destroy()
FindFirstChild
Parameters
Returns
Code Samples
local found = workspace:FindFirstChild("Brick")
if found then
found.Name = "Foo"
end
FindFirstChildOfClass
Parameters
Returns
Code Samples
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
Parameters
Returns
GetChildren
Returns
Code Samples
local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
end
GetDebugId
Parameters
Returns
Code Samples
print(workspace:GetDebugId()) --> 39FA_12
print(workspace:GetDebugId(10)) --> 39FA2FEF4D_12
print(workspace:GetDebugId(math.huge)) --> 12
GetDescendants
Returns
Code Samples
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
Returns
Code Samples
-- 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
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
IsAncestorOf
Parameters
Returns
Code Samples
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
Parameters
Returns
Code Samples
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
Remove
Returns
Code Samples
local part = Instance.new("Part")
part.Parent = workspace
print(part.Parent) --> Workspace
part:Remove()
print(part.Parent) --> nil
part.Parent = workspace
print(part.Parent) --> Workspace
WaitForChild
Parameters
Returns
Code Samples
local part = workspace:WaitForChild("Part")
print(part.Name .. " has been added to the Workspace")
children
Returns
destroy
Returns
getChildren
Returns
remove
Returns
Events
AncestryChanged
Parameters
Code Samples
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
ChildAdded
Parameters
Code Samples
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
Parameters
Code Samples
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
Parameters
Code Samples
local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspace
DescendantRemoving
Parameters
Code Samples
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
Code Samples
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()
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("In signal:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
print("Before destroying:", part:GetFullName(), #part:GetChildren())
part:Destroy()
print("After destroying:", part:GetFullName(), #part:GetChildren())