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
ตัวอย่างโค้ด
สามารถเก็บได้
local part = Instance.new("Part")
print(part:Clone()) --> Part
part.Archivable = false
print(part:Clone()) --> nilarchivable
RobloxLocked
UniqueId
Instance.UniqueId:UniqueId
วิธีการ
children
ClearAllChildren
Instance:ClearAllChildren():()
ส่งค่ากลับ
()
Clone
ส่งค่ากลับ
ตัวอย่างโค้ด
การทำสำเนา Instance
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():()
ส่งค่ากลับ
()
ตัวอย่างโค้ด
Instance:Destroy()
local part = script.Parent.Part
part:Destroy()destroy
FindFirstAncestor
FindFirstAncestorOfClass
FindFirstAncestorWhichIsA
FindFirstChild
findFirstChild
FindFirstChildOfClass
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
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
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
ส่งค่ากลับ
Variant
ตัวอย่างโค้ด
รับสไตล์
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"
-- อ้างอิงถึงปุ่ม
local button = HUDContainer:FindFirstChildWhichIsA("TextButton")
print(button:GetStyled("Rotation")) --> 0 (ค่าเริ่มต้น)
print(button.Rotation) --> 0 (ค่าเริ่มต้น)
-- ใช้การหมุนผ่านคุณสมบัติของกฎสไตล์
rule:SetProperty("Rotation", 30)
print(button:GetStyled("Rotation")) --> 30 (ค่าที่มีสไตล์ตามกฎ)
print(button.Rotation) --> 0 (ค่าเริ่มต้น)
-- แก้ไข/เขียนทับคุณสมบัติที่มีสไตล์อย่างชัดเจน
button.Rotation = 45
print(button:GetStyled("Rotation")) --> 45 (ค่าที่แก้ไข)
print(button.Rotation) --> 45 (ค่าที่แก้ไข)GetStyledPropertyChangedSignal
พารามิเตอร์
ส่งค่ากลับ
ตัวอย่างโค้ด
สัญญาณการเปลี่ยนแปลงคุณสมบัติที่มีสไตล์
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("คุณสมบัติสไตล์เปลี่ยนแปลง!")
end
meterBar:GetStyledPropertyChangedSignal("AnchorPoint"):Connect(stylePropertyChanged)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))
--> false
part.Parent = workspace
print(part:IsDescendantOf(game))
--> true
part.Parent = game
print(part:IsDescendantOf(game))
--> trueisDescendantOf
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())StyledPropertiesChanged
ตัวอย่างโค้ด
การเปลี่ยนแปลงคุณสมบัติที่มีสไตล์
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("คุณสมบัติที่มีสไตล์เปลี่ยนแปลง")
end
meterBar:StyledPropertiesChanged():Connect(stylePropertyChanged)