Instance

Hiển Thị Bản Đã Lỗi Thời

*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.

Không Thể Tạo
Không Thể Duyệt

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

  • Đọc Song Song

    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.

  • Capabilities:SecurityCapabilities
    Đọc Song Song

    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.

  • Đọc Song Song

    Một nhận dạng không duy nhất của Instance .

  • Không Sao Chép
    Đọc Song Song

    Xác định cha cấp bậc của Instance .

  • Ẩn
    Bảo Mật Plugin
    Đọc Song Song
    Đã Lỗi Thời

    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.

  • Không Sao Chép
    Đọc Song Song

    Biến instance thành một thùng chứa cát.

  • UniqueId:UniqueId
    Không Sao Chép
    Không Thể Viết Kịch Bản
    Bảo Mật Roblox
    Đọc Song Song

    Một nhận dạng duy nhất cho instance.

Phương Pháp

Sự Kiện

Thuộc Tính

Archivable

Đọc Song Song

Capabilities

SecurityCapabilities
Đọc Song Song

Name

Đọc Song Song

Parent

Không Sao Chép
Đọc Song Song

RobloxLocked

Ẩn
Bảo Mật Plugin
Đọc Song Song

Sandboxed

Không Sao Chép
Đọc Song Song

UniqueId

UniqueId
Không Sao Chép
Không Thể Viết Kịch Bản
Bảo Mật Roblox
Đọc Song Song

Phương Pháp

AddTag

()

Tham Số

tag: string
Giá Trị Mặc Định: ""

Lợi Nhuận

()

ClearAllChildren

()

Lợi Nhuận

()

Lợi Nhuận

Mẫu mã

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

()

Lợi Nhuận

()

Mẫu mã

Ví dụ: Destroy()

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

FindFirstAncestor

Ghi Song Song

Tham Số

name: string
Giá Trị Mặc Định: ""

Lợi Nhuận

FindFirstAncestorOfClass

Ghi Song Song

Tham Số

className: string
Giá Trị Mặc Định: ""

Lợi Nhuận

FindFirstAncestorWhichIsA

Ghi Song Song

Tham Số

className: string
Giá Trị Mặc Định: ""

Lợi Nhuận

FindFirstChild

Ghi Song Song

Tham Số

name: string
Giá Trị Mặc Định: ""
recursive: boolean
Giá Trị Mặc Định: false

Lợi Nhuận

Mẫu mã

Instance:FindFirstChild

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

FindFirstChildOfClass

Ghi Song Song

Tham Số

className: string
Giá Trị Mặc Định: ""

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
end

FindFirstChildWhichIsA

Ghi Song Song

Tham Số

className: string
Giá Trị Mặc Định: ""
recursive: boolean
Giá Trị Mặc Định: false

Lợi Nhuận

FindFirstDescendant

Ghi Song Song

Tham Số

name: string
Giá Trị Mặc Định: ""

Lợi Nhuận

GetActor

Ghi Song Song

Lợi Nhuận

GetAttribute

Variant
Ghi Song Song

Tham Số

attribute: string
Giá Trị Mặc Định: ""

Lợi Nhuận

Variant

GetAttributeChangedSignal

Tham Số

attribute: string
Giá Trị Mặc Định: ""

Lợi Nhuận

GetAttributes

Ghi Song Song

Lợi Nhuận

GetChildren

Instances
Ghi Song Song

Lợi Nhuận

Instances

Mẫu mã

Instance:GetChildren

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

GetDebugId

Không Thể Duyệt
Bảo Mật Plugin

Tham Số

scopeLength: number
Giá Trị Mặc Định: 4

Lợi Nhuận

Mẫu mã

Instance:GetDebugId

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

GetDescendants

Ghi Song Song

Lợi Nhuận

Mẫu mã

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

Ghi Song Song

Lợi Nhuận

Mẫu mã

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

Tham Số

name: string
Giá Trị Mặc Định: ""

Lợi Nhuận

Variant

GetStyledPropertyChangedSignal

Tham Số

property: string
Giá Trị Mặc Định: ""

Lợi Nhuận

GetTags

Ghi Song Song

Lợi Nhuận

HasTag

Ghi Song Song

Tham Số

tag: string
Giá Trị Mặc Định: ""

Lợi Nhuận

IsAncestorOf

Ghi Song Song

Tham Số

descendant: Instance
Giá Trị Mặc Định: ""

Lợi Nhuận

Mẫu mã

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

Ghi Song Song

Tham Số

ancestor: Instance
Giá Trị Mặc Định: ""

Lợi Nhuận

Mẫu mã

Ví dụ: IsDescendantOf

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

IsPropertyModified

Tham Số

property: string
Giá Trị Mặc Định: ""

Lợi Nhuận

RemoveTag

()

Tham Số

tag: string
Giá Trị Mặc Định: ""

Lợi Nhuận

()

ResetPropertyToDefault

()

Tham Số

property: string
Giá Trị Mặc Định: ""

Lợi Nhuận

()

SetAttribute

()

Tham Số

attribute: string
Giá Trị Mặc Định: ""
value: Variant
Giá Trị Mặc Định: ""

Lợi Nhuận

()

WaitForChild

Có Thể Sinh Lợi

Tham Số

childName: string
Giá Trị Mặc Định: ""
timeOut: number
Giá Trị Mặc Định: ""

Lợi Nhuận

Mẫu mã

Instance:WaitForChild

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

Sự Kiện

AncestryChanged

Tham Số

child: Instance
parent: Instance

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

Tham Số

attribute: string

ChildAdded

Tham Số

child: Instance

Mẫu mã

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

Tham Số

child: Instance

Mẫu mã

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

Tham Số

descendant: Instance

Mẫu mã

Instance.DescendantAdded

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

DescendantRemoving

Tham Số

descendant: Instance

Mẫu mã

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


Mẫu mã

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

StyledPropertiesChanged