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
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 Thể Instance
local Workspace = game:GetService("Workspace")
-- Lấy tham chiếu đến một đối tượng đã tồn tại
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 sao cho 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ã
Thành phần:Giết()
local part = script.Parent.Part
part:Destroy()destroy
FindFirstAncestor
FindFirstAncestorOfClass
FindFirstAncestorWhichIsA
FindFirstChild
findFirstChild
FindFirstChildOfClass
Tham Số
Lợi Nhuận
Mẫu mã
Thể hiện: TìmCháuĐầuTiênCủaLớp
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
GetStyledPropertyChangedSignal
Tham Số
Lợi Nhuận
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
-- Các câu lệnh này là đúng
print(Workspace:IsAncestorOf(spawnLocation))
print(Workspace:IsAncestorOf(decal))
print(spawnLocation:IsAncestorOf(decal))
-- Các 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))
--> sai
part.Parent = workspace
print(part:IsDescendantOf(game))
--> đúng
part.Parent = game
print(part:IsDescendantOf(game))
--> đúngisDescendantOf
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())