Engine Class
Instance
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
คุณสมบัติ
UniqueId:UniqueId |
วิธีการ
เหตุการณ์
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 |
สมาชิกที่ได้รับทอด
สมาชิก 6 คนที่รับทอดมาจาก Object
ได้รับทอดโดย
เอกสารอ้างอิงเกี่ยวกับ API
คุณสมบัติ
archivable
RobloxLocked
UniqueId
Instance.UniqueId:UniqueId
วิธีการ
children
ClearAllChildren
Instance:ClearAllChildren():()
ส่งค่ากลับ
()
Clone
ส่งค่ากลับ
ตัวอย่างโค้ด
การโคลนอินสแตนซ์
local Workspace = game:GetService("Workspace")
-- รับการอ้างอิงถึงวัตถุที่มีอยู่
local model = script.Parent.Model
-- สร้างโคลนของโมเดล
local clone = model:Clone()
-- ย้ายโคลนให้ไม่ทับซ้อนกับโมเดลดั้งเดิม
clone:PivotTo(model.PrimaryPart.CFrame - (Vector3.xAxis * 10))
-- เพิ่มโคลนไปยัง Workspace
clone.Parent = Workspaceclone
Destroy
Instance:Destroy():()
ส่งค่ากลับ
()
ตัวอย่างโค้ด
อินสแตนซ์:Destroy()
local part = script.Parent.Part
part:Destroy()destroy
FindFirstAncestor
FindFirstAncestorOfClass
FindFirstAncestorWhichIsA
FindFirstChild
findFirstChild
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
GetAttribute
GetAttributeChangedSignal
พารามิเตอร์
ส่งค่ากลับ
GetChildren
Instance:GetChildren():Instances
ส่งค่ากลับ
Instances
ตัวอย่างโค้ด
Instance:GetChildren
local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
endgetChildren
GetDebugId
GetDescendants
Instance:GetDescendants():Instances
ส่งค่ากลับ
Instances
ตัวอย่างโค้ด
Instance:GetDescendants
local descendants = workspace:GetDescendants()
-- วนลูปผ่านทายาททั้งหมดของ Workspace ถ้าพบ
-- BasePart โค้ดจะเปลี่ยนสีของส่วนที่นั่นเป็นสีเขียว
for _, descendant in pairs(descendants) do
if descendant:IsA("BasePart") then
descendant.BrickColor = BrickColor.Green()
end
endGetFullName
ส่งค่ากลับ
ตัวอย่างโค้ด
Instance:GetFullName
-- สร้างโครงสร้างพื้นฐานแบบง่าย
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 = "สวัสดี, โลก"
print(fire:GetFullName()) --> Workspace.Model.สวัสดี, โลก.Fireการใช้ Lua ในการนำเสนอตัวเต็ม: Instance:GetFullName
local function getFullName(object)
local result = object.Name
object = object.Parent
while object and object ~= game do
-- ใส่ชื่อของพ่อแม่ไว้ข้างหน้า
result = object.Name .. "." .. result
-- ขึ้นไปยังลำดับชั้น
object = object.Parent
end
return result
end
print(getFullName(workspace.Camera)) --> Workspace.CameraGetStyled
GetStyledPropertyChangedSignal
พารามิเตอร์
ส่งค่ากลับ
IsAncestorOf
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
Instance:IsAncestorOf()
local Workspace = game:GetService("Workspace")
local spawnLocation = Workspace.SpawnLocation
local decal = spawnLocation.Decal
-- ข้อความเหล่านี้เป็นจริง
print(Workspace:IsAncestorOf(spawnLocation))
print(Workspace:IsAncestorOf(decal))
print(spawnLocation:IsAncestorOf(decal))
-- ข้อความเหล่านี้เป็นเท็จ
print(spawnLocation:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(spawnLocation))IsDescendantOf
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
Instance:IsDescendantOf
local part = Instance.new("Part")
print(part:IsDescendantOf(game))
--> เท็จ
part.Parent = workspace
print(part:IsDescendantOf(game))
--> จริง
part.Parent = game
print(part:IsDescendantOf(game))
--> จริงisDescendantOf
IsPropertyModified
QueryDescendants
Remove
remove
ResetPropertyToDefault
SetAttribute
เหตุการณ์
AncestryChanged
ตัวอย่างโค้ด
Instance.AncestryChanged
local Workspace = game:GetService("Workspace")
local redPart = script.Parent.RedPart
local bluePart = script.Parent.BluePart
local changingPart = script.Parent.ChangingPart
-- เปลี่ยนสีของ changingPart ตาม 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} ตอนนี้ถูกตั้งเป็น Parent ที่ {parent.Name}`)
end
changingPart.AncestryChanged:Connect(onAncestryChanged)
-- ตั้งค่า Parent ของ changingPart ให้เป็นอินสแตนซ์ต่าง ๆ เมื่อเวลาผ่านไป
while true do
task.wait(2)
changingPart.Parent = redPart
task.wait(2)
changingPart.Parent = bluePart
task.wait(2)
changingPart.Parent = Workspace
endAttributeChanged
พารามิเตอร์
ChildAdded
พารามิเตอร์
ตัวอย่างโค้ด
Instance.ChildAdded
local function onChildAdded(instance)
print(instance.Name .. " ถูกเพิ่มเข้ามาใน workspace")
end
workspace.ChildAdded:Connect(onChildAdded)
local part = Instance.new("Part")
part.Parent = workspace --> Part ถูกเพิ่มเข้ามาใน WorkspacechildAdded
ChildRemoved
พารามิเตอร์
ตัวอย่างโค้ด
Instance.ChildRemoved
local function onChildRemoved(instance)
print(instance.Name .. " ถูกลบออกจาก workspace")
end
workspace.ChildRemoved:Connect(onChildRemoved)
local part = Instance.new("Part")
part.Parent = workspace
task.wait(2)
part:Destroy()DescendantAdded
พารามิเตอร์
ตัวอย่างโค้ด
Instance.DescendantAdded
local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspaceDescendantRemoving
พารามิเตอร์
ตัวอย่างโค้ด
Instance.DescendantRemoving
workspace.DescendantRemoving:Connect(function(descendant)
print(descendant.Name .. " กำลังถูกพ่อแม่เป็น " .. tostring(descendant.Parent))
end)
local part = Instance.new("Part")
part.Parent = workspace
part.Parent = nil
--> Part กำลังถูกพ่อแม่เป็น Workspace
print(part.Parent)
--> nilDestroying
ตัวอย่างโค้ด
การใช้เหตุการณ์การทำลาย (สัญญาณทันที)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("ก่อนที่จะหยุดทำงาน:", part:GetFullName(), #part:GetChildren())
task.wait()
print("หลังจากหยุดทำงาน:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
part:Destroy()การใช้เหตุการณ์การทำลาย (สัญญาณที่ล่าช้า)
local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("ในสัญญาณ:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
print("ก่อนการทำลาย:", part:GetFullName(), #part:GetChildren())
part:Destroy()
print("หลังการทำลาย:", part:GetFullName(), #part:GetChildren())