学习
引擎类
Instance
无法创建
不可浏览

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处


概要
继承成员

API 参考
属性
Archivable
读取并联
Instance.Archivable:boolean

archivable
已弃用

Capabilities
读取并联
功能: CapabilityControl
Instance.Capabilities:SecurityCapabilities

Name
读取并联
Instance.Name:string

Parent
未复制
读取并联
Instance.Parent:Instance

PredictionMode
只读
未复制
不可写入脚本
读取并联
Instance.PredictionMode:Enum.PredictionMode

RobloxLocked
已弃用

Sandboxed
未复制
读取并联
功能: CapabilityControl
Instance.Sandboxed:boolean

UniqueId
未复制
Roblox 脚本安全性
Roblox 安全性
读取并联
Instance.UniqueId:UniqueId

方法
AddTag
Instance:AddTag(tag:string):()
参数
tag:string
返回
()

children
已弃用

ClearAllChildren
Instance:ClearAllChildren():()
返回
()

Clone
功能: CreateInstances
模拟访问权限
Instance:Clone():Instance
返回
代码示例
克隆一个实例
local Workspace = game:GetService("Workspace")
-- 获取对现有对象的引用
local model = script.Parent.Model
-- 创建模型的克隆
local clone = model:Clone()
-- 移动克隆,以便它不会与原始模型重叠
clone:PivotTo(model.PrimaryPart.CFrame - (Vector3.xAxis * 10))
-- 将克隆添加到工作区
clone.Parent = Workspace

clone
已弃用

Destroy
Instance:Destroy():()
返回
()
代码示例
实例:销毁()
local part = script.Parent.Part
part:Destroy()

destroy
已弃用

FindFirstAncestor
写入并联
Instance:FindFirstAncestor(name:string):Instance?
参数
name:string
返回

FindFirstAncestorOfClass
写入并联
Instance:FindFirstAncestorOfClass(className:string):Instance?
参数
className:string
返回

FindFirstAncestorWhichIsA
写入并联
Instance:FindFirstAncestorWhichIsA(className:string):Instance?
参数
className:string
返回

FindFirstChild
写入并联
模拟访问权限
Instance:FindFirstChild(
name:string, recursive:boolean
参数
name:string
recursive:boolean
默认值:false
返回
代码示例
实例:查找第一个子对象
local found = workspace:FindFirstChild("Brick")
if found then
found.Name = "Foo"
end

findFirstChild
已弃用

FindFirstChildOfClass
写入并联
Instance:FindFirstChildOfClass(className:string):Instance?
参数
className:string
返回
代码示例
实例:查找第一个指定类的子对象
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
写入并联
模拟访问权限
Instance:FindFirstChildWhichIsA(
className:string, recursive:boolean
参数
className:string
recursive:boolean
默认值:false
返回

FindFirstDescendant
写入并联
Instance:FindFirstDescendant(name:string):Instance?
参数
name:string
返回

GetActor
写入并联
Instance:GetActor():Actor?
返回

GetAttribute
写入并联
模拟访问权限
Instance:GetAttribute(attribute:string):Variant
参数
attribute:string
返回
Variant

GetAttributeChangedSignal
Instance:GetAttributeChangedSignal(attribute:string):RBXScriptSignal
参数
attribute:string

GetAttributes
写入并联
模拟访问权限
Instance:GetAttributes():Dictionary

GetChildren
写入并联
模拟访问权限
Instance:GetChildren():Instances
返回
Instances
代码示例
实例:获取子对象
local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
end

getChildren
已弃用

GetDebugId
不可浏览
插件安全性
Instance:GetDebugId(scopeLength:number):string
参数
scopeLength:number
默认值:4
返回
代码示例
实例:获取调试 ID
print(workspace:GetDebugId()) --> 39FA_12
print(workspace:GetDebugId(10)) --> 39FA2FEF4D_12
print(workspace:GetDebugId(math.huge)) --> 12

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
end

GetFullName
写入并联
模拟访问权限
Instance:GetFullName():string
返回
代码示例
实例:获取全名
-- 创建一个简单的层次结构
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)) --> 工作区.Camera

GetStyled
Instance:GetStyled(
name:string, selector:string?
):Variant
参数
name:string
selector:string?
返回
Variant

GetStyledPropertyChangedSignal
Instance:GetStyledPropertyChangedSignal(property:string):RBXScriptSignal
参数
property:string

GetTags
写入并联
模拟访问权限
Instance:GetTags():{any}
返回

HasTag
写入并联
模拟访问权限
Instance:HasTag(tag:string):boolean
参数
tag:string
返回

IsAncestorOf
写入并联
模拟访问权限
Instance:IsAncestorOf(descendant:Instance):boolean
参数
descendant:Instance
返回
代码示例
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
写入并联
模拟访问权限
Instance:IsDescendantOf(ancestor:Instance):boolean
参数
ancestor:Instance
返回
代码示例
实例:是后代于
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

isDescendantOf
已弃用

IsPropertyModified
模拟访问权限
Instance:IsPropertyModified(property:string):boolean
参数
property:string
返回

QueryDescendants
模拟访问权限
Instance:QueryDescendants(selector:string):Instances
参数
selector:string
返回
Instances

Remove
已弃用

remove
已弃用

RemoveTag
Instance:RemoveTag(tag:string):()
参数
tag:string
返回
()

ResetPropertyToDefault
Instance:ResetPropertyToDefault(property:string):()
参数
property:string
返回
()

SetAttribute
模拟访问权限
Instance:SetAttribute(
attribute:string, value:Variant
):()
参数
attribute:string
value:Variant
返回
()

WaitForChild
可暂停
Instance:WaitForChild(
childName:string, timeOut:number
参数
childName:string
timeOut:number
返回
代码示例
实例:等待子实例
local part = workspace:WaitForChild("Part")
print(part.Name .. " 已添加到 Workspace")

活动
AncestryChanged
Instance.AncestryChanged(
child:Instance, parent:Instance
参数
child:Instance
parent:Instance
代码示例
实例.祖先改变
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
end

AttributeChanged
Instance.AttributeChanged(attribute:string):RBXScriptSignal
参数
attribute:string

ChildAdded
Instance.ChildAdded(child:Instance):RBXScriptSignal
参数
child:Instance
代码示例
实例.子项添加
local function onChildAdded(instance)
print(instance.Name .. " 添加到工作区")
end
workspace.ChildAdded:Connect(onChildAdded)
local part = Instance.new("Part")
part.Parent = workspace --> 部件添加到工作区

childAdded
已弃用

ChildRemoved
Instance.ChildRemoved(child:Instance):RBXScriptSignal
参数
child:Instance
代码示例
实例.子对象移除
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
Instance.DescendantAdded(descendant:Instance):RBXScriptSignal
参数
descendant:Instance
代码示例
实例.子孙添加
local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspace

DescendantRemoving
Instance.DescendantRemoving(descendant:Instance):RBXScriptSignal
参数
descendant:Instance
代码示例
实例.后代移除
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)
--> nil

Destroying
Instance.Destroying():RBXScriptSignal
代码示例
使用销毁事件(即时信号)
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())

StyledPropertiesChanged
Instance.StyledPropertiesChanged():RBXScriptSignal

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。