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.
Instance là lớp cơ sở cho tất cả các lớp trong cấu trúc lớp Roblox có thể là phần của cây DataModel .
Không thể tạo trực tiếp đối tượng gốc Instance , nhưng công cụ xây dựng đặc biệt Instance.new() tạo đối tượng thông qua mã, lấy tên của lớp là tham số và trả lại đối tượng được tạo.
Tóm Tắt
Thuộc Tính
Xác định xem một Instance và con cháu của nó có thể được nhân bản bằng cách sử dụng Instance:Clone() và có thể được lưu/xuất bản.
Bộ năng lực được phép sử dụng cho các kịch bản bên trong thùng chứa này.
Một nhận dạng không duy nhất của Instance .
Xác định cha cấp bậc của Instance .
Một tính năng lỗi thời đã từng được sử dụng để bảo vệ CoreGui các đối tượng.
Biến instance thành một thùng chứa cát.
Một nhận dạng duy nhất cho instance.
Phương Pháp
Áp dụng một thẻ cho instance.
Chức năng này phá hủy tất cả con của một ví dụ.
Tạo bản sao của một ví dụ và tất cả con cháu của nó, bỏ qua các ví dụ không phải Archivable .
Đặt thuộc tính Instance.Parent thành nil , khóa thuộc tính Instance.Parent , cắt tất cả các kết nối, và gọi Destroy() trên tất cả các con.
Trả về tổ tiên đầu tiên của Instance người có Instance.Name bằng tên được cung cấp.
Trả về tổ tiên đầu tiên của Instance người có Object.ClassName bằng với className đã được cung cấp.
Trả về tổ tiên đầu tiên của Instance cho người mà Object:IsA() trả về true cho className đã cho.
Trả lại đứa con đầu tiên của Instance được tìm thấy với tên đã cho.
Trả về con đầu tiên của Instance có ClassName bằng tên lớp đã cho.
Trả về con đầu tiên của Instance cho người mà Object:IsA() trả về true cho className đã cho.
Trả về con cháu đầu tiên được tìm thấy với Instance.Name được cho.
Trả về Actor liên quan đến Instances, nếu có.
Trả về giá trị đã được gán cho tên thuộc tính đã cho.
Trả về một sự kiện bắt lửa khi đặc tính được cung cấp thay đổi.
Trả về một từ điển các thuộc tính của instance.
Trả về một array chứa tất cả con của instance.
Trả về một chuỗi mã của ID gỡ lỗi được sử dụng nội bộ bởi Roblox.
Trả về một array chứa tất cả các con trai của instance.
Trả về một chuỗi mô tả nguồn gốc của instance.
Trả giá trị có định dạng hoặc được sửa đổi cụ thể của thuộc tính được chỉ định, hoặc giá trị thuộc tính mặc định nếu nó chưa được định dạng/sửa đổi.
Nhận một array của tất cả các thẻ được áp dụng cho instance.
Kiểm tra xem instance có thẻ đã cho hay không.
Trả về true nếu một Instance là tổ tiên của con cháu được cho.
Trả về true nếu một Instance là con cháu của tổ tiên được cho.
Trả về true nếu giá trị được lưu trong thuộc tính được chỉ định bằng giá trị mặc định được khởi tạo bởi mã
Loại bỏ một thẻ khỏi instance.
Đặt lại một thuộc tính về giá trị mặc định của nó.
Đặt thuộc tính với tên được cung cấp thành giá trị được cho.
Trả về con của Instance với tên được cung cấp. Nếu con không tồn tại, nó sẽ tạo luồng hiện tại cho đến khi nó làm.
Sự Kiện
Bắt lửa khi tính chất Instance.Parent của đối tượng hoặc một trong những tổ tiên của nó thay đổi.
Bắt lửa mỗi khi một thuộc tính được thay đổi trên Instance.
Lửa sau khi một đối tượng được cha trực tiếp cho đối tượng này Instance .
Lửa bùng phát sau khi một đứa trẻ bị loại bỏ khỏi đây Instance .
Lửa bùng phát sau khi một con cháu được thêm vào Instance .
Lửa bùng phát ngay lập tức trước khi một con cháu của Instance bị xóa bỏ.
Lửa bùng phát ngay lập tức trước (hoặc bị trì hoãn cho đến khi) instance bị phá hủy thông qua Instance:Destroy() .
Bắt lửa mỗi khi bất kỳ thuộc tính phong cách nào được thay đổi trên instance, bao gồm cả khi thuộc tính được đặt thành nil .
Thuộc Tính
Phương Pháp
ClearAllChildren
Lợi Nhuận
Clone
Lợi Nhuận
Mẫu mã
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
Lợi Nhuận
Mẫu mã
local part = script.Parent.Part
part:Destroy()
FindFirstChild
Tham Số
Lợi Nhuận
Mẫu mã
local found = workspace:FindFirstChild("Brick")
if found then
found.Name = "Foo"
end
FindFirstChildOfClass
Tham Số
Lợi Nhuận
Mẫu mã
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
Tham Số
Lợi Nhuận
GetAttributeChangedSignal
Tham Số
Lợi Nhuận
GetChildren
Lợi Nhuận
Mẫu mã
local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
end
GetDebugId
Tham Số
Lợi Nhuận
Mẫu mã
print(workspace:GetDebugId()) --> 39FA_12
print(workspace:GetDebugId(10)) --> 39FA2FEF4D_12
print(workspace:GetDebugId(math.huge)) --> 12
GetDescendants
Lợi Nhuận
Mẫu mã
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
Lợi Nhuận
Mẫu mã
-- 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
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
GetStyledPropertyChangedSignal
Tham Số
Lợi Nhuận
IsAncestorOf
Tham Số
Lợi Nhuận
Mẫu mã
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
Tham Số
Lợi Nhuận
Mẫu mã
local part = Instance.new("Part")
print(part:IsDescendantOf(game))
--> giả mạo
part.Parent = workspace
print(part:IsDescendantOf(game))
--> thật
part.Parent = game
print(part:IsDescendantOf(game))
--> true
SetAttribute
Tham Số
Lợi Nhuận
Sự Kiện
AncestryChanged
Tham Số
Mẫu mã
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
ChildAdded
Tham Số
Mẫu mã
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
Tham Số
Mẫu mã
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
Tham Số
Mẫu mã
local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspace
DescendantRemoving
Tham Số
Mẫu mã
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
Mẫu mã
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()
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())