Lớp công cụ
Instance
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm Tắt
Thuộc Tính
UniqueId:UniqueId |
Phương Pháp
Sự Kiện
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 |
Thành viên kế thừa
6 thành viên kế thừa từ Object
Được kế thừa bởi
Tài Liệu Tham Khảo API
Thuộc Tính
Archivable
Mẫu mã
Có thể lưu trữ
local part = Instance.new("Part")
print(part:Clone()) --> Part
part.Archivable = false
print(part:Clone()) --> nilarchivable
RobloxLocked
UniqueId
Instance.UniqueId:UniqueId
Phương Pháp
children
ClearAllChildren
Instance:ClearAllChildren():()
Lợi Nhuận
()
Clone
Lợi Nhuận
Mẫu mã
Nhân bản một Instance
local Workspace = game:GetService("Workspace")
-- Lấy tham chiếu đến một đối tượng hiện có
local model = script.Parent.Model
-- Tạo một bản sao của mô hình
local clone = model:Clone()
-- Di chuyển bản sao để nó không chồng lên mô hình gốc
clone:PivotTo(model.PrimaryPart.CFrame - (Vector3.xAxis * 10))
-- Thêm bản sao vào Workspace
clone.Parent = Workspaceclone
Destroy
Instance:Destroy():()
Lợi Nhuận
()
Mẫu mã
Instance:Destroy()
local part = script.Parent.Part
part:Destroy()destroy
FindFirstAncestor
FindFirstAncestorOfClass
FindFirstAncestorWhichIsA
FindFirstChild
findFirstChild
FindFirstChildOfClass
Tham Số
Lợi Nhuận
Mẫu mã
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
Tham Số
Lợi Nhuận
GetChildren
Instance:GetChildren():Instances
Lợi Nhuận
Instances
Mẫu mã
Instance:GetChildren
local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
endgetChildren
GetDebugId
GetDescendants
Instance:GetDescendants():Instances
Lợi Nhuận
Instances
Mẫu mã
Instance:GetDescendants
local descendants = workspace:GetDescendants()
-- Lặp qua tất cả các hậu duệ của Workspace. Nếu một
-- BasePart được tìm thấy, đoạn mã sẽ đổi màu của phần đó thành màu xanh lá cây
for _, descendant in pairs(descendants) do
if descendant:IsA("BasePart") then
descendant.BrickColor = BrickColor.Green()
end
endGetFullName
Lợi Nhuận
Mẫu mã
Instance:GetFullName
-- Tạo một hệ thống phân cấp đơn giản
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 = "Xin chào, thế giới"
print(fire:GetFullName()) --> Workspace.Model.Xin chào, thế giới.FireCài đặt Lua của Instance:GetFullName
local function getFullName(object)
local result = object.Name
object = object.Parent
while object and object ~= game do
-- Thêm tên cha vào trước
result = object.Name .. "." .. result
-- Đi lên cấu trúc phân cấp
object = object.Parent
end
return result
end
print(getFullName(workspace.Camera)) --> Workspace.CameraGetStyled
Lợi Nhuận
Variant
Mẫu mã
Lấy Kiểu
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local HUDContainer = playerGui:WaitForChild("HUDContainer")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local rule = coreSheet:FindFirstChildWhichIsA("StyleRule")
rule.Selector = "TextButton"
-- Tham chiếu đến một nút
local button = HUDContainer:FindFirstChildWhichIsA("TextButton")
print(button:GetStyled("Rotation")) --> 0 (giá trị mặc định)
print(button.Rotation) --> 0 (giá trị mặc định)
-- Áp dụng xoay qua thuộc tính quy tắc kiểu
rule:SetProperty("Rotation", 30)
print(button:GetStyled("Rotation")) --> 30 (giá trị kiểu dựa trên quy tắc)
print(button.Rotation) --> 0 (giá trị mặc định)
-- Rõ ràng sửa đổi/ghi đè thuộc tính kiểu
button.Rotation = 45
print(button:GetStyled("Rotation")) --> 45 (giá trị đã sửa đổi)
print(button.Rotation) --> 45 (giá trị đã sửa đổi)GetStyledPropertyChangedSignal
Tham Số
Lợi Nhuận
Mẫu mã
Nhận tín hiệu thay đổi thuộc tính kiểu
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("Thuộc tính kiểu đã thay đổi!")
end
meterBar:GetStyledPropertyChangedSignal("AnchorPoint"):Connect(stylePropertyChanged)IsAncestorOf
Tham Số
Lợi Nhuận
Mẫu mã
Instance:IsAncestorOf()
local Workspace = game:GetService("Workspace")
local spawnLocation = Workspace.SpawnLocation
local decal = spawnLocation.Decal
-- Những câu lệnh này là đúng
print(Workspace:IsAncestorOf(spawnLocation))
print(Workspace:IsAncestorOf(decal))
print(spawnLocation:IsAncestorOf(decal))
-- Những câu lệnh này là sai
print(spawnLocation:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(spawnLocation))IsDescendantOf
Tham Số
Lợi Nhuận
Mẫu mã
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))
--> trueisDescendantOf
IsPropertyModified
QueryDescendants
Remove
remove
ResetPropertyToDefault
SetAttribute
Sự Kiện
AncestryChanged
Mẫu mã
Instance.AncestryChanged
local Workspace = game:GetService("Workspace")
local redPart = script.Parent.RedPart
local bluePart = script.Parent.BluePart
local changingPart = script.Parent.ChangingPart
-- Thay đổi màu của changingPart dựa trên Parent của nó
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} giờ đây được gán là con của {parent.Name}`)
end
changingPart.AncestryChanged:Connect(onAncestryChanged)
-- Đặt thuộc tính Parent của changingPart thành các instance khác nhau theo thời gian
while true do
task.wait(2)
changingPart.Parent = redPart
task.wait(2)
changingPart.Parent = bluePart
task.wait(2)
changingPart.Parent = Workspace
endChildAdded
Tham Số
Mẫu mã
Instance.ChildAdded
local function onChildAdded(instance)
print(instance.Name .. " đã được thêm vào workspace")
end
workspace.ChildAdded:Connect(onChildAdded)
local part = Instance.new("Part")
part.Parent = workspace --> Phần đã được thêm vào WorkspacechildAdded
ChildRemoved
Tham Số
Mẫu mã
Instance.ChildRemoved
local function onChildRemoved(instance)
print(instance.Name .. " đã bị xóa khỏi workspace")
end
workspace.ChildRemoved:Connect(onChildRemoved)
local part = Instance.new("Part")
part.Parent = workspace
task.wait(2)
part:Destroy()DescendantAdded
Tham Số
Mẫu mã
Instance.DescendantAdded
local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspaceDescendantRemoving
Tham Số
Mẫu mã
Instance.DescendantRemoving
workspace.DescendantRemoving:Connect(function(descendant)
print(descendant.Name .. " hiện đang thuộc về " .. tostring(descendant.Parent))
end)
local part = Instance.new("Part")
part.Parent = workspace
part.Parent = nil
--> Phần hiện đang thuộc về Workspace
print(part.Parent)
--> nilDestroying
Mẫu mã
Sử dụng sự kiện Phá hủy (Tín hiệu ngay lập tức)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("Trước khi dừng lại:", part:GetFullName(), #part:GetChildren())
task.wait()
print("Sau khi dừng lại:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
part:Destroy()Sử dụng Sự kiện Hủy (Tín hiệu Hoãn)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("Trong tín hiệu:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
print("Trước khi hủy:", part:GetFullName(), #part:GetChildren())
part:Destroy()
print("Sau khi hủy:", part:GetFullName(), #part:GetChildren())StyledPropertiesChanged
Mẫu mã
Thuộc tính đã thay đổi
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("Các thuộc tính đã thay đổi")
end
meterBar:StyledPropertiesChanged():Connect(stylePropertyChanged)