Instance

显示已弃用

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

无法创建
不可浏览

实例是 Roblox 类型树的基础类,可以作为数据模型树的一部分。 不能直接创建实例对象。

实例有一个特殊函数,名为 Instance.new() ,用于通过代验证码创建对象。此函数使用类的名称作为参数,并返回创建的对象。抽象类和服务不能使用实例创建功能。

概要

属性

  • 读取并联

    决定 Instance 和它的子代是否可以使用 Instance:Clone() 进行克隆,并保存/发布。

  • Capabilities:SecurityCapabilities
    Roblox 脚本安全性
    读取并联

    允许用于此容器内的脚本的技能集。

  • 读取并联

    Class.Instance 的非独特标识。

  • 未复制
    读取并联

    确定 Instance 的父级。

  • 隐藏
    插件安全性
    读取并联
    已弃用

    一种已过期的属性,用于保护 CoreGui 对象。

  • 未复制
    Roblox 脚本安全性
    读取并联

    将实例变为沙盒容器。

  • UniqueId:UniqueId
    未复制
    不可写入脚本
    Roblox 安全性
    读取并联

方法

活动

属性

Archivable

读取并联

此属性决定是否包含体验在发布或保存时,或者调用 Clone() 上的祖先,或者调用 Clone() 直接在实例上,将返回 nil 如果该实例是 1>不1> 4> Class.

使用 复制复制 / 粘贴 选项来复制对象在 Studio 中使用 1> Class.Instance.Archivable|Archivable1> 属性,并设置 4> Class.Instance.Archivable|Archivable4> 为 7> true7> 对于复制。


local part = Instance.new("Part")
print(part:Clone()) --> Part
part.Archivable = false
print(part:Clone()) --> nil

Capabilities

SecurityCapabilities
Roblox 脚本安全性
读取并联

允许用于此实例内的脚本使用的技能集。要使技能生效,请启用 Instance.Sandboxed 属性。

此属性由实验性精选能使用。请参阅脚本功能页面获取更多信息。

Name

读取并联

Class.Instance 的非独特标识。

此属性是描述对象的标识。名称不是唯一的标识,但多个对象的子对可能会共享相同的名称。名称用于组织对象的层次结构,并且允许脚本访问特定对象。实例的名称不能超过 100 个字符。

对象的名称通常用于使用以下方法访问对象通过数据模型层次结构:


local baseplate = workspace.Baseplate
local baseplate = workspace["Baseplate"]
local baseplate = workspace:FindFirstChild("BasePlate")

使用 dot 操作将对象访问可用,必须遵循特定语法。对象名称必须以下划线或字母开头。其余的名称只能包含字母、数字或下划线(不能包含其他特殊字符)。如果对象名称不遵守此语法,它将无法使用 dot 操作和 Lua 不会将其名称解释为标识符。

如果有多个具有相同名称的对象,那么通过该名称索引一个对象的任何尝试将返回类似 Instance:FindFirstChild() 的唯一的对象,但不总是所需的对象。如果特定对象需要通过代验证码访问,它建议给它一个唯一的名称,或确保其兄弟姐妹之一不是它。如果需要通过代码访问的特定对象,它建议给它一个唯一的

注意,显示实例层级的全名可以使用 Instance:GetFullName()

Parent

未复制
读取并联

父亲属性确定Instance的父亲。 以下术语通常用于谈论这个属性的设置:

  • 一个对象是一个 子对象 ( 父对象设为 ) 一个对象的子对象。
  • Class.Instance 的子女是该对象的子女,还包括子女的子女。
  • 祖先 是Instance 的所有对象,后代 是1> Class.Instance1> 的所有对象。

它是从这个属性得到许多其他 API 成员的名称,例如 GetChildrenFindFirstChild

Class.Instance:Remove()|Remove 函数将此属性设置为空。 调用 Destroy 将父级的一个 Instance 和所有其子对象设置为 1>nil1> ,并且还会设置所有子对象的父级为 4>lock4> 。 当设置毁掉对象的父

此属性还用于管理在游戏中是否存在对象或需要移除。 只要一个对象的父级在 DataModel 中,或者通过其他对象的属性引用,就会存在在游戏中。否则,该对象将被自动移除。顶级 Class.DataModel</

使用 Instance.new() 创建的新对象将不会有父元素级,通常不会在一个设置之前可见或功能。对象的最初创建有两个步骤:创建对象,然后设置其父元素。


-- Create a part and parent it to the workspace
local part = Instance.new("Part")
part.Parent = workspace
-- Instance new can also take Parent as a second parameter
Instance.new("NumberValue", workspace)

当在 DataModel 中父级时,引用实例时可能需要执行额外的工作(例如复制、渲染和图形用户界面布局)。 当可能时,请在 父级 前更改其属性,而不是在 Class.DataModel 中设置它的父级,以避免执行此工作。

对象复制

服务器创建的对象不会在复制到客户端之前复制到客户端,直到它成为复制的某个对象的父级。当创建对象时,设置许多属性,建议设置父级为 set Parent last 。这确保对象复制一次,而不是复制许多属性更改。


local part = Instance.new("Part") -- 避免在这里使用第二个参数
part.Anchored = true
part.BrickColor = BrickColor.new("Really red")
-- 可能有很多其他的属性更改可以在这里发生…
-- 总是设置父亲最后!
part.Parent = workspace

如果您将您的零件父级到 Model 其父级尚未设置,那么将每个零件父级到该模型是否为好,因为模型还没有复制。

RobloxLocked

隐藏
插件安全性
读取并联

这个属性以前用于保护 CoreGui 服务中的对象免受用户以非授权方式改变。它已被弃用并且无法做任何事情。

Sandboxed

未复制
Roblox 脚本安全性
读取并联

将实例变为沙盒容器。沙盒容器是一个实验性功能,可以限制特定容器中的脚本执行的行为。

请参阅脚本功能页面获取更多信息。

UniqueId

UniqueId
未复制
不可写入脚本
Roblox 安全性
读取并联

方法

AddTag

void

此方法将标签添加到实例,但不会在标签已添加后发生效果。成功添加标签将发射由 CollectionService:GetInstanceAddedSignal() 创建的信号,并且使用指定的标签添加成功。

注意,标记实例时,常常有资源被用来为标签提供其功能,例如事件连接或表。为了防止内存泄露,请在不再需要标签时清理这些 (连接到信号返回的函数,或在 4>Class.CollectionService:GetInstanceRemovedSignal()</

参数

tag: string

返回

void

ClearAllChildren

void

此函数摧毁所有实例的子实例。

作为 Instance:Destroy() 也会在它使用的对象的子女上调用,此功能将会摧毁所有子后代。

清除所有孩子的替代方案

如果开发者不希望销毁所有子代,他们应该使用 Instance:GetChildren()Instance:GetDescendants() 来在对象中循环并选择要摧毁的部分。例如,下面的代码示例将摧毁所有对象中的部分。


for _, instance in object:GetDescendants() do
if instance:IsA("BasePart") then
instance:Destroy()
end
end

返回

void

代码示例

This example creates a Part and adds a few sparkle objects to the part. Then it calls Part:ClearAllChildren() to remove all of the children.

Instance:ClearAllChildren

local part = Instance.new("Part")
-- add some sparkles
for _ = 1, 3 do
local sparkles = Instance.new("Sparkles")
sparkles.Parent = part
end
print("Part has", #part:GetChildren(), "children")
--> Part has 3 children
part:ClearAllChildren()
print("Part has", #part:GetChildren(), "children")
--> Part has 0 children

克隆 创建一个克隆的实例和所有其子,无视所有不是 Archivable 的实例。 根实例的副本由此方法返回,其父实例的设

如果在克隆实例中设置了一个 ObjectValue.Value 的引用属性,复制的属性值取决于原始值:

  • 如果引用属性指向一个实例,它将指向实例。
  • 如果引用属性指向一个不是克隆的对象,复制中保留相同的值。

返回

代码示例

Demonstrates cloning a model using Instance:Clone().

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

void

Instance.Parent 属性设置为 null,锁定 Instance.Parent 属性,断开所有连接,并调用所有子弹。这是使用不需要的对象而导致的错误。扔掉不再需要的对象是重要的,因为不必要的对象和连接在一个地方使用内存(这称为一个 内存

提示: 在对象上调用摧毁后,将所有与对象相关的变量设置为空。这使您的代码无法访问与对象有关的任何内容。


local part = Instance.new("Part")
part.Name = "Hello, world"
part:Destroy()
-- 不要这样做:
print(part.Name) --> “你好,世界”
-- 这样做可以防止上述行从工作:
part = nil

一旦 Instance 被此方法摧毁,它就无法被重用,因为 Instance.Parent 属性已锁定。要暂时移除对象,请将 Parent 设置为零。例如:


object.Parent = nil
wait(2)
object.Parent = workspace

要在一段时间后摧毁对象,请使用 Debris:AddItem()


返回

void

代码示例

Demonstrates destroying a Part using the Instance:Destroy() function.

This function is the correct way to dispose of objects that are no longer required.

Instance:Destroy()

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

FindFirstAncestor

写入并联

返回 Instance 的第一个祖先,其 Instance.Name 与所给出的名称相等。

此函数从实例的即时 Instance.Parent 开始,并向 DataModel 工作。如果找不到匹配的上游,它将返回 nil。

以下代码示例将找到名为“Car”的对象的第一个祖先。


local car = object:FindFirstAncestor("Car")

对于特定类型的变体,请参阅Instance:FindFirstAncestorOfClass()Instance:FindFirstAncestorWhichIsA()

参数

name: string

要查找的 Instance.Name


返回

找到了 Instance

FindFirstAncestorOfClass

写入并联

返回 Instance 的第一个祖先,其 Object.ClassName 与指定的父级相等。

此函数从实例的即时 Instance.Parent 开始,并向 DataModel 工作。如果找不到匹配的上游,它将返回 nil。

使用此函数的常见用途是查找 ModelBasePart 属于。例如:


local model = part:FindFirstAncestorOfClass("Model")

此函数是 Instance:FindFirstAncestor() 的变体,它检查 Object.ClassName 属性而不是 Instance.Name 。 1> Class.Instance:FindFirstAncestorWhichIsA()1> 也存在,使用 4> Class.Object:IsA()4> 方法而

参数

className: string

要查找的 Object.ClassName


返回

找到了 Instance

FindFirstAncestorWhichIsA

写入并联

返回 Instance 的第一个祖先,其父辈为 Object:IsA() ,其父辈为 Class.Instance 。

此函数从实例的即时 Instance.Parent 开始,并向 DataModel 工作。如果找不到匹配的上游,它将返回 nil。

Instance:FindFirstAncestorOfClass() 不同,此函数使用 Object:IsA() ,这是尊重类继承的。例如:


print(part:IsA("Part")) --> true
print(part:IsA("BasePart")) --> true
print(part:IsA("Instance")) --> true

因此,以下代码示例将返回第一个 BasePart 祖先,无论它是 WedgePartMeshPart 或 2>Class.Part2> 。


local part = object:FindFirstAncestorWhichIsA("BasePart")

还请参阅,Instance:FindFirstAncestor()

参数

className: string

要查找的 Object.ClassName


返回

找到了 Instance

FindFirstChild

写入并联

返回 Instance 中的第一个子实例,

nil 如果不存在此类子。如果可选的 recursive 参数是真的,此函数会搜索所有后代,而不仅仅是 Instance 的直接子女。

检查对象的存在

如果您需要验证对象是否存在,找到第一个子女是必要的。使用 dot 操作索引子女的名称,如果子女不存在,将会发生错误。


-- 如果零件不存在在工作区中,将发生以下错误:
workspace.Part.Transparency = 0.5

使用 FindFirstChild 检查零件,然后使用 if 语句运行需要它的代码。


local part = workspace:FindFirstChild("Part")
if part then
part.Transparency = 0.5
end

寻找一个子子的名称与属性匹配

有时,Name 一个对象的相同,Parent 的属性。 使用 dot 操作时,属性取得优先权过子女,如果它们共享一个名称。

在下面的例子中,一个名为 “Color” 的 Folder 添加到一个 Part ,其也有 Part.Color 属性。 1>Class.Part.Color1> 指向 4>Datatype.Color34> ,而不是 Folder。


local part = Instance.new("Part")
local folder = Instance.new("Folder")
folder.Name = "Color"
folder.Parent = part
local c = part.Color --> Color3
local c2 = part:FindFirstChild("Color") --> The Folder

使用 FindFirstChild() 这样使用的好处是,新属性的引入不会对您的验证码构成风险。

性能注释

FindFirstChild() 比使用 dot 操作多大约 20% 或使用存储在 Class.RunService.Heartbeat

参数

name: string

要搜索的 Instance.Name

recursive: bool

是否进行递归搜索。

默认值:false

返回

找到了 Instance

代码示例

The below would look in Workspace for an object name "Brick". If found, it will change the name of the object to "Foo".

Instance:FindFirstChild

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

FindFirstChildOfClass

写入并联

返回 Instance 的第一个子 ClassName 其 Class.Object.className|className 与指定的子 1>Class.Instance1> 相等。

如果找不到匹配的子,这个函数将返回 nil。

Instance:FindFirstChildWhichIsA() 不同,此函数仅返回具有指定类型的对象,无视类型继承。

寻找儿子的开发者应该使用 Instance:FindFirstChild() 而不是。

参数

className: string

要查找的 Object.ClassName


返回

找到了 Instance

代码示例

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

写入并联

返回 Instance 中的第一个子 Object:IsA() 为其返回 Class.Object:IsA 给予的真实值。

如果找不到与子对应的子,此函数将返回 nil。如果选项递归参数是真的,此函数将搜索所有子辈子,而不仅是 Instance 的直接子。

Instance:FindFirstChildOfClass() 不同,此函数使用 Object:IsA(),这是尊重类继承的。例如:


print(part:IsA("Part")) --> 是
print(part:IsA("BasePart")) --> 是
print(part:IsA("Instance")) --> true

因此,以下代码示例将返回第一个 BasePart 子,无论它是 WedgePartMeshPart 或 2>Class.Part2> 。


local part = object:FindFirstChildWhichIsA("BasePart")

开发者正在寻找名称为“Class.Instance:FindFirstChild”的孩子,应该使用 Instance:FindFirstChild()

参数

className: string

要搜索的 Object.ClassName

recursive: bool

是否进行递归搜索。

默认值:false

返回

找到了 Instance

FindFirstDescendant

写入并联

返回找到的第一个后代,使用 Instance.Name

此方法已禁用,不能使用。要找到实例的第一个子代,请考虑使用 recursive 参数在 Instance:FindFirstChild() 上。

参数

name: string

Class.Instance.Name 以搜索。


返回

找到了 Instance

GetActor

写入并联

如果 Instance 是一个 Actor ,那么 Class.Actor 本身将返回。否则,它最近的祖先 2>Class.Actor2> 将返回。如果没有祖先是一个 5>Class.Actor5> ,结果将是 8>nil8> 。


返回

找到了 Actor

GetAttribute

Variant
写入并联

此方法返回指定的属性名称下的值。如果没有属性被指定,nil 将返回。

例如,以下代码片段将设置,然后取得实例的 初始位置 属性的值:


local part = workspace.Part
part:SetAttribute("InitialPosition", part.Position)
local initialPosition = instance:GetAttribute("InitialPosition")
print(initialPosition)

还见

参数

attribute: string

正在检索的属性名。


返回

Variant

指定给予的属性名的值。如果没有属性被指定,nil 将返回。

GetAttributeChangedSignal

此函数返回一个与 Changed 事件完全相同的事件,除了它只会在特定的属性更改时发生外,它其实相当于 GetPropertyChangedSignal() 但对属性的情况。

一般来说,使用此方法而不是使用连接到 Changed 与具有相同属性名称的函数来检查属性名称是一个好主意。后续对此方法在同一对象上使用相同属性名称的调用将返回相同的事件。

以下代码示例返回一个信号,该信号激活 attributeChanged() 函数,当零件的 初始位置 属性发生变更:


local part = workspace.Part
part:SetAttribute("InitialPosition", part.Position)
local function attributeChanged()
print("Attribute changed")
end
part:GetAttributeChangedSignal("InitialPosition"):Connect(attributeChanged)

还参阅Instance.AttributeChanged,这会每次更改实例上的属性时触发。

参数

attribute: string

更改信号正在返回的特定属性的名称。


返回

指定属性变更时触发的事件。

GetAttributes

写入并联

此方法为每个属性返回一个包含键值对的字典,其中键值是属性的名称,值是非nil 的值。

例如,以下代码片段输出实例的属性和值:


local part = workspace.Part
part:SetAttribute("InitialPosition", part.Position)
part:SetAttribute("CanUse", true)
for name, value in part:GetAttributes() do
print(name .. " = " .. value)
end

还参阅 Instance:GetAttribute() ,该值返回指定属性名称下分配的值。


返回

对于每个属性,串的名称和变体是不同的,这些属性的变体也是不同的。

GetChildren

Instances
写入并联

返回一个包含所有实例直接子女的数阵(数值索引的表),或者所有 Instance 的实例,其 Parent 与对象相等。使用任何一个数值或 genéric 的 for-Loop 来访问时,可以在递归上使用一个索引。


-- 示例数学 for-loop
local children = workspace:GetChildren()
for i = 1, #children do
local child = children[i]
print(child.Name .. " is child number " .. i)
end

-- 通用循环示例
local children = workspace:GetChildren()
for i, child in children do
print(child.Name .. " is child number " .. i)
end

孩子们按其Parent属性设置对象的顺序进行排序。

还请参阅 GetDescendants 函数。


返回

Instances

包含实例子女的阵列。

代码示例

The below would print the name of all objects currently in Workspace when ran.

Instance:GetChildren

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

GetDebugId

不可浏览
插件安全性

返回 Roblox 使用内部的调试 ID 编码的字符串。

注意:

  • 此物品受到保护。 尝试在 ScriptLocalScript 中使用它会导致错误
  • 调试ID是用于调试过程中使用的ID。它允许调试器在应用程序处理它们之前读取每个说明。所有对象在 Roblox 中都作为过程使用,如果需要,可以调试。
  • 这可能对于需要将相似对象区分从一个到另一个的插件(例如共享同一个名称的对象)有帮助

参数

scopeLength: number

范围长度。

默认值:4

返回

调试ID字符串。

代码示例

Instance:GetDebugId

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

GetDescendants

写入并联

此对象方法返回一个包含该对象所有子对象的阵列。与 Instance:GetChildren() 不同,它只返回对象的即时子对象,此方法找到每个子对象,每个子对象的子对象,并且如此继续。


返回

包含实例子的阵列。

代码示例

GetDescendants is often used to do something to all the descendants that are a particular type of object. The code in this example uses GetDescendants and Instance:IsA() to find all of the parts in the workspace and turns them green.

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

写入并联

返回一个描述实例先祖的字工作间串。字符串是对对象和其父辈的 Name 的 concatenation 的 DataModel ( game ) 。 不考虑 2>Class.Part2> 在 5>Class.Workspace5>

当调用一个 Instance 它不是 DataModel 的子代时,此函数考虑所有上游至和包括最高级的一级无上游父辈。

此函数用于日志和调试。您不应尝试解析返回的字符串以获得任何有用的操作;此函数不会逃避期间(或其他符号)在对象名称中。 在其他 words,虽然其输出经常看起来是一个有效的 Lua 识别器,但它不是保证。


返回

Class.Instance 的完整名称。

代码示例

This code sample demonstrates the behavior of Instance:GetFullName(). It shows how the function behaves when called on an object not in the DataModel hierarchy, and it also shows how the return value does not escape special characters.

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

This code sample re-implements the Instance:GetFullName() function in Lua.

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

参数

name: string

返回

Variant

GetTags

写入并联

此方法将应用于给定实例的标签阵列返回为字符串,您可以在 Studio 中的属性窗口或在运行时使用AddTag()添加标签。

此方法有助于您想要在多个标签上同时执行某些操作时使用。 但是,它的效率不高,因此您不能使用此方法来检查单个标签的存在;相反,您可以使用 HasTag() 来检查特定标签。


返回

HasTag

写入并联

此方法将返回 true 如果提供的标签已添加到对象。您可以在 Studio 中的 属性 窗口或在运行时使用 AddTag() 添加标签。

参数

tag: string

返回

IsAncestorOf

写入并联

如果 Instance 是给定后代的祖先,它将返回 true。

Class.Instance 被视为对象的先祖,如果对象的 Instance.Parent 或其父父元素的 Instance.Parent 设置为 1>Class.Instance1> 。

还请参阅, Instance:IsDescendantOf()

参数

descendant: Instance

后裔 Instance


返回

Class.Instance 是给定后代的祖先。

代码示例

Demonstrates determining if one instance is the ancestor of another using Instance:IsAncestorOf()

Workspace and SpawnLocation are ancestors of the SpawnLocation's decal. Workspace is an ancestor of SpawnLocation.

SpawnLocation and its decal are descendants of Workspace, not ancenstors. Decal is a descendant to SpawnLocation, not an ancestor.

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

写入并联

如果 Instance 是给定祖先的后代,它将返回 true。

Class.Instance 是一个对实例的子对象,如果父对象或其父元素对象设置为对象,那么它就是一个子对象。

注意,DataModel是零的子。这意味着IsDescendantOf无法用零参数检查对象是否已移除。

还请参阅,Instance:IsAncestorOf()

参数

ancestor: Instance

上始祖先 Instance .


返回

Class.Instance 是否是后代,取决于给定的上祖先。

代码示例

Instance:IsDescendantOf

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

RemoveTag

void

此方法从实例中移除标签。如果对象没有标签,将不会发生错误。成功移除标签将触发由 CollectionService:GetInstanceRemovedSignal() 创建的信号,并且标记标签。

注意,标记实例时,常常有一些资源被用来为标签提供其功能,例如事件连接或表。为了防止内存泄露,它的好 idea是清理这些 (连接到 nil 等) 当不再需要标签时。

参数

tag: string

返回

void

SetAttribute

void

此方法将给定的名称设置到指定的值。如果指定的值为 nil ,该属性将被移除,因为 nil 是默认值。

例如,以下代码片段将实例的 初始位置 属性设置为 Vector3.new(0, 10, 0) :


local part = workspace.Part
part:SetAttribute("InitialPosition", Vector3.new(0, 10, 0))

限制

命名要求和限制:

  • 名称只能使用字母数字字符和下划线。
  • 不允许空格或独特符号。
  • 字符串必须少于 100 个字符。
  • 名称不允许开始以 RBX ,除非调用者是 Roblox 核心脚本(仅对于 Roblox)。

尝试将属性设置为不支持的类输入时,将发生错误。

还请参阅:

参数

attribute: string

设置的属性名称。

value: Variant

用于设置指定属性的值。


返回

void

WaitForChild

可暂停

返回 Instance 中给定的名称为子。如果子不存在,它将返回当前线程直到它做到。如果 timeOut 参数指定,此方法将在指定的时间后结束并返回 nil

主要使用

WaitForChild() 是在使用 LocalScript 中的客户端代码运行时的关键时间。 当 Roblox 引擎设置 Class.Workspace.Stream

注释

  • 这个函数不会生成,如果调用时存在给定名称的子。
  • Instance:FindFirstChild() 是一个更有效的替换WaitForChild() 对于假设存在的对象。
  • 如果调用此方法超过 5 秒,而不返回,并且没有指定 timeOut 参数,警告将被打印到输出,这可能会导致线程无法在definitely的时间内返回。

参数

childName: string

要查找的 Instance.Name

timeOut: number

可选的时间参数。


返回

找到了 Instance

代码示例

The following code waits for an instance named "Part" to be added to Workspace.

Instance:WaitForChild

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

活动

AncestryChanged

当对象或其父辈的 Instance.Parent 属性发生变更时触发。

此事件包括两个参数,childparentchild 指向的是实际上被更改的1> Class.Instance1> 的其4> Class.Instance.Parent4>。7> parent7> 指向这个实例的新0> Class.Instance.Parent0>。

您可以使用此事件来跟踪 Studio 中的实例删除,例如通过 Explorer 手动删除或通过插件。如果您需要检测使用 Instance:Destroy() 来销毁实例时,请使用 Instance.Destroying 事件。

参数

child: Instance

Class.Instance 的父级 Instance.Parent 已更改。

parent: Instance

Class.Instance 的新 Instance,其 Instance.Parent 已更改。


代码示例

Demonstrates detecting changes to an instance's ancestry by connecting to the Instance.AncestryChanged event.

The ChangingPart's Parent is set to different values overtime. The parent of the part is the part's ancestor, so the Instance.AncestryChanged event will fire whenever it changes.

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

这个事件会触发,无论实例上的属性是否设置为 nil 。该改变的属性名称将传递给连接的函数。

例如,以下代码片段将 attributeChanged() 函数连接到零件的属性变更时,发射:


local part = workspace.Part
local function attributeChanged(attributeName)
print(attributeName, "changed")
end
part.AttributeChanged:Connect(attributeChanged)

还请参阅 Instance:GetAttributeChangedSignal() ,这会返回一个发生特定属性变更时触发的事件。

参数

attribute: string

更改的属性的名称。


ChildAdded

在对象成为此 Instance 的父级后发射。

注意,使用此函数检测服务器创建的对象时,必须使用 Instance:WaitForChild() 来索引这些对象的子对象。这是因为对象和子对象不保证从服务器到客户端同时复制。例如:


workspace.ChildAdded:Connect(function(child)
-- need to use WaitForChild as descendants may not have replicated yet
local head = child:WaitForChild("Head")
end)

注意,此功能仅适用于Instance的即时子女。对于捕获所有子代的函数,请使用Instance.DescendantAdded

还请参阅, Instance.ChildRemoved

参数

child: Instance

已添加的 Instance


代码示例

This snippet prints the names of objects as they are added to the Workspace:

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

在此 Instance 中移除孩子后发射。

移除引用于当一个对象的父亲从此 Instance 更改为此 Instance 之外的任何东西。注意,此事件还会在一个子对象被销毁(使用 Instance:Destroy())时触发,因为销毁函数将一个对象的父亲设置为零。

此函数仅适用于Instance的即时子女。对于捕获所有子女的函数,请使用Instance.DescendantRemoving

还请参阅 Instance.ChildAdded

参数

child: Instance

已移除的 Instance


代码示例

This snippet prints the names of objects as they are removed from the Workspace:

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

子女添加事件会在子女被添加到Instance后触发。

作为每个后代代添加火焰为每个子代,父亲对对象的子代发生 Instance 将为此对象和所有子代个别地发生事件。

开发人员仅关注 Instance 的即时子女应该使用 Instance.ChildAdded

请参阅Instance.DescendantRemoving

参数

descendant: Instance

已添加的 Instance


代码示例

This following example will print the name of any object that is added to the Workspace:

Instance.DescendantAdded

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

DescendantRemoving

此事件将在父级 Class.Instance 更改之前立即触发,使得一个 Instance 实例不再是一个后代。 Class.Instance:Destroy()|Destroy() 更改父级的 2>Class.Instance.Parent|Parent2> 为 5>nil5>,因此调用此方法

该事件在该后代女被删除之前触发,因此该子女的父亲在该事件发生时不会发生变更。如果该子女是父父元素的直接子女,此事件将在Class.Instance.ChildRemoved 之前发生。

如果子女有孩子,这个事件会先发生在子女上,然后在子女下。

警告

这个事件会触发与被移除的对象相关的后裔对象。尝试将 Parent 的后裔设置为其他东西会失败。以下是一个示例,该示例显示了这一点:


workspace.DescendantRemoving:Connect(function(descendant)
-- Do not manipulate the parent of the descendant in this function!
-- This event fires BECAUSE the parent was manipulated, and the change hasn't happened yet
-- Therefore, it is problematic to change the parent like this:
descendant.Parent = game
end)
local part = Instance.new("Part")
part.Parent = workspace
part.Parent = nil

还请参阅DescendantAdded

参数

descendant: Instance

正在移除的 Instance


代码示例

The following example prints the name of any descendant as it is being removed from the Workspace:

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

Class.Instance 将永远不会从内存中删除,因为连接的函数仍在使用它。但如果函数在任何时候生成,Instance 和其子将被父级为 nil

如果 Workspace.SignalBehavior 属性设置为 Enum.SignalBehavior.Immediate,此事件即将在 Instance 或其父辈被摧毁使用 1> Class.Instance:Destroy()1> 时立即触发。

如果 Workspace.SignalBehavior 属性设置为 Enum.SignalBehavior.Deferred ,此事件将在下一个恢复点触发,该恢复点位于 Instance 或其父级被摧毁使用 1> Class.Instance:Destroy()1> 。

使用 Deferred 行为,连接脚本到自己的 Instance.Destroying 事件是问题,因为脚本将在调用回调之前被销毁(意味着它将不会执行)。

在 Studio 中删除 Instance 时,例如通过 Explorer 手动删除或通过插件 Instance 不会被销毁。 相反,父级是设置为 2>nil2> ,您可以跟踪它使用 5>Class.Instance.AncestryChanged5> 。


代码示例

This sample demonstrates how, when using Immediate signal behavior, an Instance being destroyed remains in place until the connected function yields.

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

This sample demonstrates how, when using Deferred signal behavior, an Instance is destroyed before the signal fires.

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