Instance

Afficher les obsolètes

*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.

Création impossible
Non navigable

Instance est la classe de base pour toutes les classes dans la hiérarchie de classe Roblox qui peuvent faire partie de l'arbre DataModel.

Il n'est pas possible de créer directement des objets racine Instance , mais le constructeur spécial Instance.new() crée des objets via du code, en prenant le nom de la classe en paramètre et en retournant l'objet créé.

Résumé

Propriétés

  • Lecture parallèle

    Détermine si un Instance et ses descendants peuvent être clonés à l'aide de Instance:Clone() et s'ils peuvent être sauvegardés/publiés.

  • Capabilities:SecurityCapabilities
    Lecture parallèle

    Le jeu de capacités pouvant être utilisé pour les scripts à l'intérieur de ce conteneur.

  • Lecture parallèle

    Un identifiant non unique du Instance .

  • Non répliqué
    Lecture parallèle

    Détermine le parent hiérarchique du Instance .

  • Caché
    Sécurité des plugins
    Lecture parallèle
    Déprécié

    Une propriété obsolète qui protégeait autrefois les objets CoreGui.

  • Non répliqué
    Lecture parallèle

    Transforme l'instance en conteneur sandboxé.

  • UniqueId:UniqueId
    Non répliqué
    Non scriptable
    Sécurité Roblox
    Lecture parallèle

    Un identifiant unique pour l'instance.

Méthodes

Événements

Propriétés

Archivable

Lecture parallèle

Capabilities

SecurityCapabilities
Lecture parallèle

Name

Lecture parallèle

Parent

Non répliqué
Lecture parallèle

RobloxLocked

Caché
Sécurité des plugins
Lecture parallèle

Sandboxed

Non répliqué
Lecture parallèle

UniqueId

UniqueId
Non répliqué
Non scriptable
Sécurité Roblox
Lecture parallèle

Méthodes

AddTag

()

Paramètres

tag: string
Valeur par défaut : ""

Retours

()

ClearAllChildren

()

Retours

()

Retours

Échantillons de code

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

()

Retours

()

Échantillons de code

Instance:Destroy()

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

FindFirstAncestor

Écrire en parallèle

Paramètres

name: string
Valeur par défaut : ""

Retours

FindFirstAncestorOfClass

Écrire en parallèle

Paramètres

className: string
Valeur par défaut : ""

Retours

FindFirstAncestorWhichIsA

Écrire en parallèle

Paramètres

className: string
Valeur par défaut : ""

Retours

FindFirstChild

Écrire en parallèle

Paramètres

name: string
Valeur par défaut : ""
recursive: boolean
Valeur par défaut : false

Retours

Échantillons de code

Instance : FindFirstChild

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

FindFirstChildOfClass

Écrire en parallèle

Paramètres

className: string
Valeur par défaut : ""

Retours

Échantillons de code

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

Écrire en parallèle

Paramètres

className: string
Valeur par défaut : ""
recursive: boolean
Valeur par défaut : false

Retours

FindFirstDescendant

Écrire en parallèle

Paramètres

name: string
Valeur par défaut : ""

Retours

GetActor

Écrire en parallèle

Retours

GetAttribute

Variant
Écrire en parallèle

Paramètres

attribute: string
Valeur par défaut : ""

Retours

Variant

GetAttributeChangedSignal

Paramètres

attribute: string
Valeur par défaut : ""

Retours

GetAttributes

Écrire en parallèle

Retours

GetChildren

Instances
Écrire en parallèle

Retours

Instances

Échantillons de code

Instance:GetChildren

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

GetDebugId

Non navigable
Sécurité des plugins

Paramètres

scopeLength: number
Valeur par défaut : 4

Retours

Échantillons de code

Instance:GetDebugId

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

GetDescendants

Écrire en parallèle

Retours

Échantillons de code

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

Écrire en parallèle

Retours

Échantillons de code

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

GetStyled

Variant

Paramètres

name: string
Valeur par défaut : ""

Retours

Variant

GetStyledPropertyChangedSignal

Paramètres

property: string
Valeur par défaut : ""

Retours

GetTags

Écrire en parallèle

Retours

HasTag

Écrire en parallèle

Paramètres

tag: string
Valeur par défaut : ""

Retours

IsAncestorOf

Écrire en parallèle

Paramètres

descendant: Instance
Valeur par défaut : ""

Retours

Échantillons de code

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

Écrire en parallèle

Paramètres

ancestor: Instance
Valeur par défaut : ""

Retours

Échantillons de code

Instance : IsDescendantOf

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

IsPropertyModified

Paramètres

property: string
Valeur par défaut : ""

Retours

RemoveTag

()

Paramètres

tag: string
Valeur par défaut : ""

Retours

()

ResetPropertyToDefault

()

Paramètres

property: string
Valeur par défaut : ""

Retours

()

SetAttribute

()

Paramètres

attribute: string
Valeur par défaut : ""
value: Variant
Valeur par défaut : ""

Retours

()

WaitForChild

Peut augmenter

Paramètres

childName: string
Valeur par défaut : ""
timeOut: number
Valeur par défaut : ""

Retours

Échantillons de code

Instance:WaitForChild

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

Événements

AncestryChanged

Paramètres

child: Instance
parent: Instance

Échantillons de code

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

Paramètres

attribute: string

ChildAdded

Paramètres

child: Instance

Échantillons de code

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

Paramètres

child: Instance

Échantillons de code

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

Paramètres

descendant: Instance

Échantillons de code

Instance.DescendantAdded

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

DescendantRemoving

Paramètres

descendant: Instance

Échantillons de code

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


Échantillons de code

Using the Destroying Event (Immediate signals)

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()
Using the Destroying Event (Deferred signals)

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())

StyledPropertiesChanged