Classe de moteur
Instance
*Ce contenu est traduit en utilisant l'IA (Beta) et peut contenir des erreurs. Pour consulter cette page en anglais, clique ici.
Résumé
Propriétés
UniqueId:UniqueId |
Méthodes
Événements
AncestryChanged(child: Instance,parent: Instance):RBXScriptSignal |
AttributeChanged(attribute: string):RBXScriptSignal |
ChildAdded(child: Instance):RBXScriptSignal |
childAdded(child: Instance):RBXScriptSignal |
ChildRemoved(child: Instance):RBXScriptSignal |
DescendantAdded(descendant: Instance):RBXScriptSignal |
DescendantRemoving(descendant: Instance):RBXScriptSignal |
Membres hérités
6 hérité du Object
Hérité par
Référence API
Propriétés
archivable
RobloxLocked
UniqueId
Instance.UniqueId:UniqueId
Méthodes
children
ClearAllChildren
Instance:ClearAllChildren():()
Retours
()
Clone
Retours
Échantillons de code
Cloner une Instance
local Workspace = game:GetService("Workspace")
-- Obtenir une référence à un objet existant
local model = script.Parent.Model
-- Créer un clone du modèle
local clone = model:Clone()
-- Déplacer le clone afin qu'il ne chevauche pas le modèle original
clone:PivotTo(model.PrimaryPart.CFrame - (Vector3.xAxis * 10))
-- Ajouter le clone au Workspace
clone.Parent = Workspaceclone
Destroy
Instance:Destroy():()
Retours
()
Échantillons de code
Instance:Destroy()
local part = script.Parent.Part
part:Destroy()destroy
FindFirstAncestor
FindFirstAncestorOfClass
FindFirstAncestorWhichIsA
FindFirstChild
findFirstChild
FindFirstChildOfClass
Paramètres
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
endFindFirstChildWhichIsA
FindFirstDescendant
GetAttributeChangedSignal
Paramètres
Retours
GetChildren
Instance:GetChildren():Instances
Retours
Instances
Échantillons de code
Instance:GetChildren
local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
endgetChildren
GetDebugId
GetDescendants
Instance:GetDescendants():Instances
Retours
Instances
Échantillons de code
Instance:GetDescendants
local descendants = workspace:GetDescendants()
-- Parcourir tous les descendants de l'espace de travail. Si un
-- BasePart est trouvé, le code change la couleur de cette pièce en vert
for _, descendant in pairs(descendants) do
if descendant:IsA("BasePart") then
descendant.BrickColor = BrickColor.Green()
end
endGetFullName
Retours
Échantillons de code
Instance:GetFullName
-- Créer une hiérarchie simple
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 = "Bonjour, le monde"
print(fire:GetFullName()) --> Workspace.Model.Bonjour, le monde.FireInstance:GetFullName Implémentation Lua
local function getFullName(object)
local result = object.Name
object = object.Parent
while object and object ~= game do
-- Préfixer le nom du parent
result = object.Name .. "." .. result
-- Monter dans la hiérarchie
object = object.Parent
end
return result
end
print(getFullName(workspace.Camera)) --> Workspace.CameraGetStyled
GetStyledPropertyChangedSignal
Paramètres
Retours
IsAncestorOf
Paramètres
Retours
Échantillons de code
Instance:IsAncestorOf()
local Workspace = game:GetService("Workspace")
local spawnLocation = Workspace.SpawnLocation
local decal = spawnLocation.Decal
-- Ces déclarations sont vraies
print(Workspace:IsAncestorOf(spawnLocation))
print(Workspace:IsAncestorOf(decal))
print(spawnLocation:IsAncestorOf(decal))
-- Ces déclarations sont fausses
print(spawnLocation:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(spawnLocation))IsDescendantOf
Paramètres
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))
--> vraiisDescendantOf
IsPropertyModified
QueryDescendants
Remove
remove
ResetPropertyToDefault
SetAttribute
Événements
AncestryChanged
É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
-- Changez la couleur de changingPart en fonction de son 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} est maintenant parenté à {parent.Name}`)
end
changingPart.AncestryChanged:Connect(onAncestryChanged)
-- Définissez la propriété Parent de changingPart sur différentes instances au fil du temps
while true do
task.wait(2)
changingPart.Parent = redPart
task.wait(2)
changingPart.Parent = bluePart
task.wait(2)
changingPart.Parent = Workspace
endAttributeChanged
Paramètres
ChildAdded
Paramètres
Échantillons de code
Instance.ChildAdded
local function onChildAdded(instance)
print(instance.Name .. " ajouté à l'espace de travail")
end
workspace.ChildAdded:Connect(onChildAdded)
local part = Instance.new("Part")
part.Parent = workspace --> Partie ajoutée à l'espace de travailchildAdded
ChildRemoved
Paramètres
Échantillons de code
Instance.ChildRemoved
local function onChildRemoved(instance)
print(instance.Name .. " retiré du workspace")
end
workspace.ChildRemoved:Connect(onChildRemoved)
local part = Instance.new("Part")
part.Parent = workspace
task.wait(2)
part:Destroy()DescendantAdded
Paramètres
Échantillons de code
Instance.DescendantAdded
local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspaceDescendantRemoving
Paramètres
Échantillons de code
Instance.DescendantRemoving
workspace.DescendantRemoving:Connect(function(descendant)
print(descendant.Name .. " est actuellement parenté à " .. tostring(descendant.Parent))
end)
local part = Instance.new("Part")
part.Parent = workspace
part.Parent = nil
--> La pièce est actuellement parentée au Workspace
print(part.Parent)
--> nilDestroying
Échantillons de code
Utilisation de l'événement de destruction (signaux immédiats)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("Avant de se bloquer :", part:GetFullName(), #part:GetChildren())
task.wait()
print("Après s'être bloqué :", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
part:Destroy()Utilisation de l'événement Destroying (signaux différés)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("Dans le signal :", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
print("Avant la destruction :", part:GetFullName(), #part:GetChildren())
part:Destroy()
print("Après la destruction :", part:GetFullName(), #part:GetChildren())