概要
属性
UniqueId:UniqueId |
方法
活动
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 |
继承成员
6 个继承自 Object
API 参考
属性
archivable
RobloxLocked
UniqueId
Instance.UniqueId:UniqueId
方法
children
ClearAllChildren
Instance:ClearAllChildren():()
返回
()
Clone
返回
代码示例
克隆一个实例
local Workspace = game:GetService("Workspace")
-- 获取对现有对象的引用
local model = script.Parent.Model
-- 创建模型的克隆
local clone = model:Clone()
-- 移动克隆,以便它不会与原始模型重叠
clone:PivotTo(model.PrimaryPart.CFrame - (Vector3.xAxis * 10))
-- 将克隆添加到工作区
clone.Parent = Workspaceclone
Destroy
Instance:Destroy():()
返回
()
代码示例
实例:销毁()
local part = script.Parent.Part
part:Destroy()destroy
FindFirstAncestorOfClass
FindFirstAncestorWhichIsA
FindFirstChild
findFirstChild
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
GetAttributeChangedSignal
参数
GetChildren
Instance:GetChildren():Instances
返回
Instances
代码示例
实例:获取子对象
local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
endgetChildren
GetDebugId
GetDescendants
Instance:GetDescendants():Instances
返回
Instances
代码示例
实例:获取后代
local descendants = workspace:GetDescendants()
-- 遍历工作区的所有后代。如果找到一个
-- BasePart,代码将该部件的颜色更改为绿色
for _, descendant in pairs(descendants) do
if descendant:IsA("BasePart") then
descendant.BrickColor = BrickColor.Green()
end
endGetFullName
返回
代码示例
实例:获取全名
-- 创建一个简单的层次结构
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 = "你好,世界"
print(fire:GetFullName()) --> Workspace.Model.你好,世界.Fire实例:获取完整名称的 Lua 实现
local function getFullName(object)
local result = object.Name
object = object.Parent
while object and object ~= game do
-- 添加父级名称
result = object.Name .. "." .. result
-- 向上移动层级
object = object.Parent
end
return result
end
print(getFullName(workspace.Camera)) --> 工作区.CameraGetStyled
GetStyledPropertyChangedSignal
参数
IsAncestorOf
参数
返回
代码示例
Instance:IsAncestorOf()
local Workspace = game:GetService("Workspace")
local spawnLocation = Workspace.SpawnLocation
local decal = spawnLocation.Decal
-- 这些语句为真
print(Workspace:IsAncestorOf(spawnLocation))
print(Workspace:IsAncestorOf(decal))
print(spawnLocation:IsAncestorOf(decal))
-- 这些语句为假
print(spawnLocation:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(spawnLocation))IsDescendantOf
isDescendantOf
Remove
remove
SetAttribute
活动
AncestryChanged
代码示例
实例.祖先改变
local Workspace = game:GetService("Workspace")
local redPart = script.Parent.RedPart
local bluePart = script.Parent.BluePart
local changingPart = script.Parent.ChangingPart
-- 根据其 Parent 更改 changingPart 的颜色
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} 现在的父级是 {parent.Name}`)
end
changingPart.AncestryChanged:Connect(onAncestryChanged)
-- 随着时间的推移,将 changingPart 的 Parent 属性设置为不同的实例
while true do
task.wait(2)
changingPart.Parent = redPart
task.wait(2)
changingPart.Parent = bluePart
task.wait(2)
changingPart.Parent = Workspace
endChildAdded
参数
代码示例
实例.子项添加
local function onChildAdded(instance)
print(instance.Name .. " 添加到工作区")
end
workspace.ChildAdded:Connect(onChildAdded)
local part = Instance.new("Part")
part.Parent = workspace --> 部件添加到工作区childAdded
ChildRemoved
参数
代码示例
实例.子对象移除
local function onChildRemoved(instance)
print(instance.Name .. " 从工作区移除")
end
workspace.ChildRemoved:Connect(onChildRemoved)
local part = Instance.new("Part")
part.Parent = workspace
task.wait(2)
part:Destroy()DescendantAdded
参数
代码示例
实例.子孙添加
local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspaceDescendantRemoving
参数
代码示例
实例.后代移除
workspace.DescendantRemoving:Connect(function(descendant)
print(descendant.Name .. " 当前父级为 " .. tostring(descendant.Parent))
end)
local part = Instance.new("Part")
part.Parent = workspace
part.Parent = nil
--> 部件当前父级为工作区
print(part.Parent)
--> nilDestroying
代码示例
使用销毁事件(即时信号)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("暂停前:", part:GetFullName(), #part:GetChildren())
task.wait()
print("暂停后:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
part:Destroy()使用销毁事件(延迟信号)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("在信号中:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
print("在销毁之前:", part:GetFullName(), #part:GetChildren())
part:Destroy()
print("在销毁之后:", part:GetFullName(), #part:GetChildren())