Instance 是 Roblox 類別階層中所有類別的基礎類,可以成為 DataModel 樹的一部分。
無法直接創建根 Instance 對象,但特殊 Instance.new() 建造器通過代碼創建對象,將類別名稱作為參數並返回創建的對象。
概要
屬性
決定是否可以使用 Instance 複製某個 Instance:Clone() 和其子孫,並可保存/發布。
允許使用此容器內的腳本的能力集。
不是唯一標識符的 Instance 。
決定 Instance 的階層父。
一個已過時的屬性,用於保護 CoreGui 對象。
將實例變成沙盒容器。
案例的獨特標識符。
方法
對實例應用標籤。
此功能會摧毀所有實例的子孫。
創建一個複製的實例和所有其子孫,忽略不是 Archivable 的實例。
將 Instance.Parent 屬性設為 nil , 鎖定 Instance.Parent 屬性, 切斷所有連線, 並在所有子孫上呼叫 Destroy() 。
返回 Instance 的第一個祖先,其 Instance.Name 與給定的名稱相等。
返回 Instance 的第一個祖先,其 Object.ClassName 與指定的 className 相等。
返回第一個祖先的 Instance 對於誰 Object:IsA() 返回給指定類別的真值。
返回找到指定名稱的 Instance 首個兒童。
返回 Instance 的第一個孩子,對於 Object:IsA() 返回真實的給定類別。
返回找到的第一個子孫,使用指定的 Instance.Name 。
返回與實例相關的 Actor ,如果有。
返回指定的特性名稱所指定的值。
返回在指定特性變更時發生的事件。
返回實例的特性辭典。
返回包含所有實例子女的數組。
返回 Roblox 內部使用的調試 ID 的編碼字串。
返回包含所有實例後裔的數陣。
返回描述實例祖先的字串。
返回指定的屬性的裝飾或明確修改的值,或者如果尚未裝飾/修改,則為預設屬性值。
獲得適用於實例的所有標籤的數組。
檢查是否有給定標籤的實例。
如果 Instance 是給定後裔的祖先,返回真值。
返回 true 如果 Instance 是給定祖先的後裔。
如果指定的屬性中儲存的值與代碼所啟動的預設值相等,返回 true 如果值儲存在指定的屬性中,則返回值儲存在指定的屬性中。
從實例中移除標籤。
將一個屬性重設為其預設值。
將具有給定名稱的特性設為給定值。
返回 Instance 的兒子以指定的名稱。如果兒子不存在,它將生成當前線程直到它做為止。
活動
屬性
Archivable
此屬性決定是否應該將實例包含在體驗發布或儲存時,或當 Clone() 在實例祖先中呼叫時,實例應包含在體驗中。直接呼叫Clone()在實例上將返回nil如果該實例不是 不 Archivable。
使用 複製 或 複製 / 粘貼 選項複製工作室中的對象將忽略其自己的 Archivable 屬性並設置 Archivable 為 true 的副本。
local part = Instance.new("Part")print(part:Clone()) --> Partpart.Archivable = falseprint(part:Clone()) --> nil
Capabilities
允許使用此實例內的腳本的能力集。若要啟用功能,Instance.Sandboxed屬性必須啟用。
此特性由實驗功能使用。請見 腳本功能 獲得更多詳情。
Name
不是唯一標識符的 Instance 。名稱用於維持對象層次結構,同時允許腳本存取特定對象。實例名稱不能超過 100 個字元的大小。
對象名稱通常用於通過數據模型階層使用以下方法存取對象:
local Workspace = game:GetService("Workspace")local baseplate = Workspace.Baseplatelocal baseplate = Workspace["Baseplate"]local baseplate = Workspace:FindFirstChild("BasePlate")
為了使用點運算符 ( . ) 存取對象,其名稱必須以下劃線或字母開始,剩餘名稱只能包含字母、數字或下劃線 (沒有其他特殊字符)如果物件名稱不遵循此語法,它將無法使用點運算符並且 Luau 將不會將其名稱解釋為標示符。
如果同一名稱的多個對象是兄弟姐妹,以那個名稱索引一個對象的任何嘗試都會返回僅一個對象,類似於 Instance:FindFirstChild() ,但不總是所需的對象。如果需要通過代碼存取特定對象,建議給予它獨特的名稱或保證其姐妹之一沒有相同的名稱。
也可以參考 Instance:GetFullName() 以獲得包含對象層級的完整名稱。
Parent
Parent 屬性決定了 Instance 的階層父。以下術語在討論如何設置此屬性時常用:
對象是 兒子 的,或是 被設為那個對象 的另一個對象,當其 Parent 設為那個對象時。
一個 子孫 的下一級是該對象的子孫,還有子孫的子孫。
一個 祖先 的所有對象都是實例的後裔。
它來自 Parent 屬性,許多其他 API 會員獲得名稱,例如 GetChildren() 和 FindFirstChild() 。此屬性也用於管理是否對體驗中的對象進行刪除或需要刪除。只要物件的父在 DataModel 中,並存在變數中,或被其他物件的屬性引用,物件就會留在體驗中;否則,物件將會自動移除。
呼叫 Destroy() 將設置 Parent 的 Instance 和所有子孫的 nil 為 **** ,並且 Parent 屬性。當設置已摧毀對象的 Parent 時發生錯誤。
使用 Instance.new() 創建的新對象通常不會有父親,並且通常不會在一個被設置之前顯示或功能。
物件複製
由伺服器創建的對象在複製到某個被複製的對象之前不會複製到客戶端。當創建對象並設置許多屬性時,建議設置 Parent 屬性 last 。這樣可以確保對象只複製一次,而不是複製許多屬性變更。
local Workspace = game:GetService("Workspace")-- 設定新實例的父親最後(建議)local part = Instance.new("Part")part.Position = Vector3.new(0, 10, 0)part.Parent = Workspace
然而,如果還沒有設置父親部分到 Model 的父親,將每個部分都設置為該模型是可接受的,因為模型不會複製。
方法
AddTag
此方法對實例應用標籤,如果標籤已應用,沒有效果。成功添加標籤會發射由 CollectionService:GetInstanceAddedSignal() 創建的信號,使用指定的標籤。
警告
客戶端添加的標籤將在伺服器稍後在該實例上添加或移除標籤時掉落,因為伺服器會將所有標籤複製並覆蓋以前的標籤。
當標記實例時,常見的是某些資源被使用來提供標籤的功能,例如事件連線或表。為了防止記憶洩漏,當標籤不再需要時,清理這些(切斷、設為 nil 等)是一個好主意。當呼叫 Instance:RemoveTag() 、呼叫 Instance:Destroy() 或在與 CollectionService:GetInstanceRemovedSignal() 返回的信號連接的函數中執行時,請執行此操作。
參數
返回
ClearAllChildren
此功能會摧毀所有實例的子孫和後裔。
local part = Instance.new("Part")-- Add some sparklesfor i = 1, 3 dolocal sparkles = Instance.new("Sparkles")sparkles.Parent = partlocal sc = Instance.new("Sparkles")sc.Parent = sparklesendprint("Children:", #part:GetChildren()) --> Children: 3part:ClearAllChildren()print("Children:", #part:GetChildren()) --> Children: 0
如果您不希望毀滅所有 兒女 和後裔,請使用 Instance:GetChildren() 或 Instance:GetDescendants() 來循環那些兒女/後裔,然後選擇要毀滅的內容。例如,下列代碼示例將摧毀所有從 BaseParts 下降的 Model :
local Workspace = game:GetService("Workspace")local model = Workspace:FindFirstChild("TestModel")for _, descendant in model:GetDescendants() doif descendant:IsA("BasePart") thendescendant:Destroy()endend
返回
Clone
Clone() 創建一個複製的實例和所有其子孫,忽略所有不是 Archivable 的實例。這個方法會返回根實例的副本,其 Parent 設為 nil。請注意,如果實例本身設為 Archivable 設為 false,此功能將返回 nil。
如果參考屬性,例如 ObjectValue.Value 在複製的實例中設置,複製屬性的值取決於原始屬性的值:
- 如果參考屬性指向複製了 也 複製的實例,複製將指向複製。
- 如果參考屬性指向一個未被複製的對象,相同值將在複製中保留。
返回
範例程式碼
Demonstrates cloning a model using Instance:Clone().
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
將 Instance.Parent 屬性設為 nil , 鎖定 Instance.Parent 屬性, 切斷所有連線, 並在所有子孫上呼叫 Destroy() 。此功能是丟棄不再需要的對象的正確方式。
卸下不需要的物件很重要,因為位置中不需要的物件和連線會使用記憶,長期下來可能會導致嚴重的效能問題。
在呼叫 Destroy() 對對象後,將對象(或其子對象)的任何變量設置為 nil 。這樣可以防止您的代碼存取與對象有關的任何東西。
local part = Instance.new("Part")part.Name = "Hello, world"part:Destroy()-- 不要這麼做:print(part.Name) --> "你好,世界"-- 這麼做以防止上面的行不工作:part = nil
一旦使用此方法摧毀了 Instance ,由於 Instance.Parent 屬性被鎖定,無法重複使用。若要暫時 移除對象而不是毀滅它 ,請將 Parent 設為 nil。例如:
local Workspace = game:GetService("Workspace")object.Parent = niltask.wait(2)object.Parent = Workspace
若要在一段時間後摧毀對象,請使用 Debris:AddItem() 。
返回
範例程式碼
展示使用 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() 。
參數
要尋找的 Instance.Name 。
返回
FindFirstAncestorOfClass
返回 Instance 的第一個祖先,其 Object.ClassName 與指定的 className 相等。
此功能向上工作,意味著它從即時的 Instance.Parent 開始,並向 DataModel 工作。如果找不到符合的祖先,它將返回 nil 。
這個功能的常見用途是找到 Model 一個 BasePart 屬於。例如:
local model = part:FindFirstAncestorOfClass("Model")
此功能是 Instance:FindFirstAncestor() 的變體,會檢查 Object.ClassName 屬性而不是 Instance.Name 。Instance:FindFirstAncestorWhichIsA() 也存在,使用 Object:IsA() 方法來尊重類繼承。
參數
要尋找的 Object.ClassName 。
返回
FindFirstAncestorWhichIsA
返回第一個祖先的 Instance 對於誰 Object:IsA() 返回給指定類別的真值。
此功能向上工作,意味著它從即時的 Instance.Parent 開始,並向 DataModel 工作。如果找不到符合的祖先,它將返回 nil 。
與 Instance:FindFirstAncestorOfClass() 不同,此功能使用 Object:IsA(),這會尊重類繼承。例如:
print(part:IsA("Part")) --> trueprint(part:IsA("BasePart")) --> trueprint(part:IsA("Instance")) --> true
因此,下列代碼示例將返回第一個 BasePart 祖先,無論是 WedgePart、MeshPart 或 Part 。
local part = object:FindFirstAncestorWhichIsA("BasePart")
也見 Instance:FindFirstAncestor() 。
參數
要尋找的 Object.ClassName 。
返回
FindFirstChild
返回 Instance 的第一個兒童以指定的名稱,或 nil 如果沒有此兒童存在。如果可選擇的 recursive 參數是 true ,這個函數會搜尋所有子孫,而不是只搜尋 Instance 的直接兒女。
檢查對象的存在
FindFirstChild() 必須如果你需要在繼續前驗證對象存在。使用點符運作器嘗試以名稱索引子女時,如果子女不存在,就會發生錯誤。
local Workspace = game:GetService("Workspace")-- 如果零件不存在工作區,下列錯誤線將出現Workspace.Part.Transparency = 0.5
一個更好的方法是使用 FindFirstChild() 首先檢查 Part ,然後使用 if 語句執行需要它的代碼。
local Workspace = game:GetService("Workspace")local part = Workspace:FindFirstChild("Part")if part thenpart.Transparency = 0.5end
尋找名稱與屬性匹配的兒童
有時候對象的 Name 與其屬性的 Parent 相同。當使用點符運作器時,屬性會在名稱上優先於子孫,如果它們共享名稱。
在下面的例子中,一個 被添加到 ,這也有 屬性。注意到 part.Color 指的是 Color3 屬性值,而不是兒子 Folder 實例。使用 FindFirstChild() 的好處是,新屬性的引入不會對您的代碼造成風險。
local part = Instance.new("Part")local folder = Instance.new("Folder")folder.Name = "Color"folder.Parent = partlocal c1 = part.Color -- 屬性local c2 = part:FindFirstChild("Color") -- The child folder
性能說明
FindFirstChild() 比使用點運算符多花費約 20% 時間,比純粹儲存對象的引用長近 8 倍。因此,您應避免在緊密循環或連接到 RunService.Heartbeat 和 RunService.PreRender 的功能中呼叫它。取而代之,將結果儲存在變量中,或考慮使用 ChildAdded 或 WaitForChild() 來偵測給定名稱的兒子何時可用。
參數
要搜尋的 Instance.Name 。
是否應該使用鏈接查找進行搜尋。
返回
範例程式碼
The below would look in Workspace for an object name "Brick". If found, it will change the name of the object to "Foo".
local found = workspace:FindFirstChild("Brick")
if found then
found.Name = "Foo"
end
FindFirstChildOfClass
返回 Instance 的第一個孩子,其 ClassName 與給定的 className 相等。與 Instance:FindFirstChildWhichIsA() 不同,此函數只返回符合 className 類型的對象,忽略類型繼承。如果找不到符合的子,此功能將返回 nil 。
參數
要尋找的 Object.ClassName 。
返回
範例程式碼
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() 返回真實的給定類別。
如果找不到符合的子,此功能將返回 nil 。如果可選擇的递归參數是真的,這個函數會搜索所有子孫,而不是只搜索 Instance 的即時兒女。
與 Instance:FindFirstChildOfClass() 不同,此功能使用 Object:IsA(),這會尊重類繼承。例如:
print(part:IsA("Part")) --> 真print(part:IsA("BasePart")) --> 真print(part:IsA("Instance")) --> true
因此,下列代碼示例將返回第一個 BasePart 子,無論是 WedgePart、MeshPart 或 Part 。
local part = object:FindFirstChildWhichIsA("BasePart")
尋找名稱的兒童的開發人員應該使用 Instance:FindFirstChild() 取代。
參數
要搜尋的 Object.ClassName 。
是否應該使用鏈接查找進行搜尋。
返回
FindFirstDescendant
返回找到的第一個子孫,使用指定的 Instance.Name 。
此方法已停用且無法使用。若要找到實例的第一個後裔,請考慮使用 recursive 參數在 Instance:FindFirstChild() 上。
參數
要搜索的 Instance.Name 。
返回
GetAttribute
此方法返回指定給特定屬性名稱的值。如果沒有指定屬性,nil將被返回。
例如,下面的代碼片段設置並取得實例的 InitialPosition 屬性值:
local Workspace = game:GetService("Workspace")local part = Workspace.Partpart:SetAttribute("InitialPosition", part.Position)local initialPosition = instance:GetAttribute("InitialPosition")print(initialPosition)
也見「也見」
- Instance:SetAttribute() 設置具有給定名稱的屬性為給定值。
- Instance:GetAttributes() ,返回每個實例特性的鑰值對陣列典字典。
參數
正在擷取的特性名稱。
返回
被指派給給定特性名稱的值。如果沒有指派特性,nil將被返回。
GetAttributeChangedSignal
這個功能返回一個事件,行為與 Changed 事件相同,除了只有在指定的特定屬性發生變更時才會發射;有效地,它與 GetPropertyChangedSignal() 類似,但僅適用於屬性。
一般來說,使用此方法來代替連接到 Changed 以檢查特性名稱的功能是一個好主意。對同一個對象上的相同名稱的相同特性的後續呼叫此方法返回相同事件。
下面的代碼示例返回一個信號,當零件的 InitialPosition 特性變更時發射功能 attributeChanged() :
local Workspace = game:GetService("Workspace")
local part = Workspace.Part
part:SetAttribute("InitialPosition", part.Position)
local function attributeChanged()
print("Attribute changed")
end
part:GetAttributeChangedSignal("InitialPosition"):Connect(attributeChanged)
也可以參考 Instance.AttributeChanged ,它會在任何時候變更實例上的任何特性時發射。
參數
變更訊號返回的指定特性名稱。
返回
當指定的特性變更時發生的事件。
GetAttributes
這個方法返回每個特性中,鑰匙是特性名稱,值是非 nil 值的鑰值對陣辭典。
例如,下面的代碼片段輸出一個實例的特性和值:
local Workspace = game:GetService("Workspace")local part = Workspace.Partpart:SetAttribute("InitialPosition", part.Position)part:SetAttribute("CanUse", true)for name, value in part:GetAttributes() doprint(name .. " = " .. value)end
也可以參閱Instance:GetAttribute(),它會返回被指定給給定特性名稱的值。
返回
一個字典,包含弦 → 變體對每個特性的名稱,弦是非零值的變體。
GetChildren
返回包含所有直接兒女的 array(數字索引的表)或每個 Instance 其 Parent 與對象相同的所有子兒女。可以使用數字或一般的 for 循環來循環陣列:
local Workspace = game:GetService("Workspace")-- 數字循環示例local children = Workspace:GetChildren()for i = 1, #children dolocal child = children[i]print(child.Name .. " is child number " .. i)end
local Workspace = game:GetService("Workspace")-- 一般循環示例local children = Workspace:GetChildren()for i, child in children doprint(child.Name .. " is child number " .. i)end
孩子們依照其 Parent 屬性被設置到對象的順序排序。
也參見 GetDescendants 功能。
返回
包含實例兒子的數組。
範例程式碼
The below would print the name of all objects currently in Workspace when ran.
local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
end
GetDebugId
返回由 Roblox 內部使用的調試 ID 編碼的字串。請注意:
- 此項目受保護。嘗試在 Script 或 LocalScript 中使用將導致錯誤。
- 偵錯 ID 是用於偵錯過程的 ID。它允許調試器在應用程序執行之前閱讀每個指令。Roblox 中的所有對象都像過程一樣,每次運行指令(或「代碼」)都可以在需要時進行調試。
- 這可能對需要區分相似對象的插件(例如擁有相同名稱的對象)有幫助。
參數
範圍長度。
返回
調試ID字串。
範例程式碼
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.
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 的連接,用期間隔開。不考慮 DataModel ( game )。例如,Part 在 Workspace 中可能返回 Workspace.Part 。
當在 Instance 上呼叫不是 DataModel 的子孫時,此函數會考慮所有祖先,直到最頂尖的一個沒有 Parent 。
此功能對於記錄和偵錯有用。你不應該嘗試解析返回的字串以進行任何有用的操作;此功能不會逃避對象名稱中的時間點 (或其他符號)。換言之,雖然其輸出經常看起來是一個有效的 Luau 標示符,但它不是保證的。
返回
範例程式碼
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.
-- 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.
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
此方法返回指定的屬性的 сти列或明確修改的值,或者如果尚未被 сти列/修改,則為預設屬性值。與直接存取屬性值不同,例如 [GuiObject].Rotation ,會返回屬性的預設或修改值。
local Players = game:GetService("Players")local ReplicatedStorage = game:GetService("ReplicatedStorage")local player = Players.LocalPlayerlocal playerGui = player.PlayerGuilocal HUDContainer = playerGui:WaitForChild("HUDContainer")local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")local rule = coreSheet:FindFirstChildWhichIsA("StyleRule")rule.Selector = "TextButton"-- 參考按鈕local button = HUDContainer:FindFirstChildWhichIsA("TextButton")print(button:GetStyled("Rotation")) --> 0 (預設值)print(button.Rotation) --> 0 (預設值)-- 通過風格規則屬性應用旋轉功能rule:SetProperty("Rotation", 30)print(button:GetStyled("Rotation")) --> 30 (按規則樣式的值)print(button.Rotation) --> 0 (預設值)-- 明確修改/覆蓋樣式屬性button.Rotation = 45print(button:GetStyled("Rotation")) --> 45(修改值)print(button.Rotation) --> 45 (modified value)
參數
要查詢的屬性名稱。
返回
指定的屬性的造型或明確修改的值,或者如果尚未造型/修改,則為預設屬性值。
GetStyledPropertyChangedSignal
這個方法返回一個事件,行為與 StyledPropertiesChanged 事件相同,除了只在指定風格屬性變更時發射。一般來說,使用此方法來代替連接到 StyledPropertiesChanged 以檢查屬性名稱的函數是一個好主意。對同一個物件上相同名稱的後續呼叫此方法返回相同的事件。
請注意,此事件不會傳送任何參數到連接的函數,因此變更的屬性值必須直接在腳本中閱讀。
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local HUDContainer = playerGui:WaitForChild("HUDContainer")
local meterBar = HUDContainer.MeterBar
local function stylePropertyChanged()
print("Style property changed!")
end
meterBar:GetStyledPropertyChangedSignal("AnchorPoint"):Connect(stylePropertyChanged)
參數
要聆聽變更的風格屬性名稱。
返回
當指定風格屬性變更時發生的事件。
GetTags
此方法返回給定實例應用的標籤的一個數陣,作為字串。您可以在 Studio 的 屬性 窗口或在執行時使用 AddTag() 添加標籤。
當您想要一次使用多個標籤對實例進行操作時,此方法很有用。然而,使用此方法來檢查單個標籤的存在是不高效的;相反,使用 HasTag() 來檢查特定標籤。
返回
IsAncestorOf
如果 Instance 是給定後裔的祖先,返回真值。
一個 Instance 被視為對象的祖先,如果對象的 Instance.Parent 或其父親之一的 Instance.Parent 設置為 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.
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
返回 true 如果 Instance 是給定祖先的後裔。
請注意,IsDescendantOf() 無法使用 nil 參數來檢查對象是否已移除。
參數
返回
範例程式碼
local part = Instance.new("Part")
print(part:IsDescendantOf(game))
--> 否
part.Parent = workspace
print(part:IsDescendantOf(game))
--> 真
part.Parent = game
print(part:IsDescendantOf(game))
--> true
IsPropertyModified
如果指定的屬性中儲存的值與代碼啟動的預設值相等,返回 true ;如果指定的屬性沒有儲存值,則返回空白。例如,如果輸出 屬性的 指示 ,那麼在標籤上呼叫 將返回 因為 是標籤創建時的預設值,而不是通過 Studio 插入工作流程插入的。
請注意,如果此方法返回 true , 格式將 不會 影響屬性,因為明確修改屬性會取代它的格式。
參數
要查詢的屬性名稱。
返回
指示是否修改屬性的 boolean 。
RemoveTag
此方法從實例中移除標籤。如果對象沒有標籤,它不會發出錯誤。成功移除標籤會發射由 CollectionService:GetInstanceRemovedSignal() 創建的信號,使用指定的標籤。
注意,當標記實例時,常見的是某些資源被用來提供標籤的功能,例如事件連線或表。為了防止記憶洩漏,當標籤不再需要時,清理這些(切斷、設為 nil 等)是一個好主意。
參數
返回
ResetPropertyToDefault
將一個屬性重設為其預設值。例如,呼叫 ResetPropertyToDefault("Rotation") 在 TextLabel 上與將其 Rotation 設置為 0 (屬性的預設值) 相等。這個方法可以用來確保 сти样將覆蓋此屬性的預設值。
參數
要重設的屬性名稱。
返回
SetAttribute
此方法將指定名稱的屬性設為指定值。如果給出的值是 nil ,則屬性將被移除,因為 nil 會預設返回。
例如,以下代碼片段設置實例的 InitialPosition 屬性為 Vector3.new(0, 10, 0):
local Workspace = game:GetService("Workspace")local part = Workspace.Partpart:SetAttribute("InitialPosition", Vector3.new(0, 10, 0))
限制
命名要求和限制:
- 名稱只能使用字母數字和橫條。
- 不允許使用空格或獨特符號。
- 字串必須少於 100 個字元。
- 名稱不得以 RBX 開始,除非呼叫者是 Roblox 核心腳本(僅供 Roblox 使用)。
當嘗試將特性設為未支援類型時,將發生錯誤。
也見「也見」
- Instance:GetAttribute() ,返回指定給特定名稱的值。
- Instance:GetAttributes() ,返回每個實例特性的鑰值對陣列典字典。
參數
正在設置的特性名稱。
設定指定特性的值。
返回
WaitForChild
返回 Instance 的孩子以指定名稱。如果孩子不存在,它會在它做到之前產生當前線程。如果指定 timeOut 參數,此方法將在指定的秒數後過期並返回 nil 。
主要使用
WaitForChild() 在 LocalScript 中運行的客戶端代碼時工作時,與代碼運行有關的極為重要。Roblox 引擎不保證對象從伺服器複製到客戶端的時間或順序。此外,如果體驗設置為Workspace.StreamingEnabled,那麼遠離玩家角色的BaseParts可能不會傳送到客戶端,可能導致客戶端上的對象尚未存在時導致腳本斷裂。
注意事項
- 此功能在呼叫時,如果給定名稱的兒子存在,就不會產生。
- Instance:FindFirstChild() 是對假設存在的對象的更有效的替代方案 WaitForChild() 。
- 如果呼叫此方法超過 5 秒而未返回,且未指定任何 timeOut 參數,將印出輸出上警告說線程可能會永久生成。
參數
要尋找的 Instance.Name 。
可選擇的時間限制參數。
返回
範例程式碼
The following code waits for an instance named "Part" to be added to Workspace.
local part = workspace:WaitForChild("Part")
print(part.Name .. " has been added to the Workspace")
活動
AncestryChanged
當物件的 Instance.Parent 屬性或其祖先之一變更時,發生火災。
此事件包含兩個參數:引用到了的變更了其的,而引用到了這個實例的新的。
您可以使用此事件來跟蹤 Studio 中實例的刪除,例如手動刪除在 Explorer 或通過插件來刪除。如果您需要使用 Instance:Destroy() 偵測實例是否被摧毀,請使用 Instance.Destroying 事件。
參數
其中 Instance 的 Instance.Parent 已被更改。
新的 Instance.Parent 的 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.
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 Workspace = game:GetService("Workspace")
local part = Workspace.Part
local function attributeChanged(attributeName)
print(attributeName, "changed")
end
part.AttributeChanged:Connect(attributeChanged)
也可以參閱Instance:GetAttributeChangedSignal(),它會返回一個事件,當特定給定的屬性發生變更時發射。
參數
被更改的特性名稱。
ChildAdded
在對象被指派到此 Instance 之後發生火災。
注意,當在客戶端使用此功能來偵測伺服器創建的對象時,必須使用 Instance:WaitForChild() 來索引這些對象的子孫。這是因為對象和其子孫不保證同時從伺服器複製到客戶端。例如:
local Workspace = game:GetService("Workspace")
Workspace.ChildAdded:Connect(function(child)
-- Use WaitForChild() since descendants may not have replicated yet
local head = child:WaitForChild("Head")
end)
注意,此功能只適用於 Instance 的即時兒女。對於捕捉所有後裔的函數,請使用 Instance.DescendantAdded 。
參數
範例程式碼
This snippet prints the names of objects as they are added to the Workspace:
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 的即時兒女。對於捕捉所有後裔的函數,請使用 Instance.DescendantRemoving 。
也見 Instance.ChildAdded 。
參數
範例程式碼
This snippet prints the names of objects as they are removed from the Workspace:
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 。
參數
範例程式碼
以下範例將列印工作區中添加的任何對象名稱:
local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspace
DescendantRemoving
這個事件會在父變更之前立即發射子女實例,使得子女實例不再是子女。Destroy() 變更一個實例的 Parent 到 nil ,因此調用父親的子孫上該方法會導致此事件發生。
由於此事件在子孫被移除之前發射 ,因此在此事件發射時,子孫的父親將不會發生變更。如果子孫也是父親的直接兒女,此事件將在 之前發射。
如果後裔有兒女,此事件首先發射到後裔,然後是它的兒女。
警告
此事件發射到正在被移除的子對象。嘗試將子孫的 Parent 設為其他東西將失敗。以下是一個示例來展示這點:
local Workspace = game:GetService("Workspace")
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 。
參數
範例程式碼
The following example prints the name of any descendant as it is being removed from the Workspace:
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
連接的功能仍在使用它時,Instance 將永遠不會從記憶中刪除。然而,如果函數在任何時候產生,其子孫 Instance 和其子孫將被傳送到 nil 。
如果 Workspace.SignalBehavior 屬性設為 Enum.SignalBehavior.Immediate ,此事件會在 Instance 或其祖先之一被摧毀前立即發生 Instance:Destroy() 。
如果 Workspace.SignalBehavior 屬性設為 Enum.SignalBehavior.Deferred ,此事件將在下一次重新啟動點發生,這將是在 Instance 或其祖先之一被摧毀後 Instance:Destroy() 之後。
使用 Deferred 行為時,連接腳本到自己的 Instance.Destroying 事件會有問題,因為腳本將在回調可以呼叫之前被摧毀(即不會執行)。
當在 Studio 刪除 Instance 時,例如手動通過 檢索器 或通過插件刪除,Instance 不會被摧毀。相反,父親設為 nil ,您可以使用 Instance.AncestryChanged 來跟蹤。
範例程式碼
This sample demonstrates how, when using Immediate signal behavior, an Instance being destroyed remains in place until the connected function yields.
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.
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
此事件會在實例上變更任何風格屬性時發生,包括當屬性設為 nil 時。
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local HUDContainer = playerGui:WaitForChild("HUDContainer")
local meterBar = HUDContainer.MeterBar
local function stylePropertyChanged()
print("Styled properties changed")
end
meterBar:StyledPropertiesChanged():Connect(stylePropertyChanged)