BasePart

顯示已棄用項目

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡

無法建立
無法瀏覽

BasePart 是一個抽象基礎類別,用於在世界中渲染和物理模擬時的對象。在 Workspace 中。有多種實現 BasePart ,最常見的是 PartMeshPart 。其他包括 WedgePart , SpawnLocation , 和單例 Terrain 對物件。一般來說,當文件引用「部分」時,大多數 BasePart 實現將工作,而不只是 Part

有關如何將 BaseParts 組合成模擬剛體的信息,請參閱 裝配

有許多不同的對象與 BasePart (除了 Terrain ) 互動,包括:

概要

屬性

屬性 繼承自 PVInstance

方法

方法 繼承自 PVInstance
  • 平行寫入

    獲得 PVInstance 的軸心。

  • PivotTo(targetCFrame : CFrame):()

    將 以及所有其子孫 轉換為指定的 位置,使旋轉點現在位於指定的 位置。

活動

屬性

Anchored

平行讀取

Anchored 屬性決定零件是否會被物理學移除。啟用時,零件將永遠不會因重力、其他零件碰撞、疊加其他零件或任何其他與物理相關的原因而改變位置。因結果,兩個被錨定的零件永遠不會對彼此發射 Touched 事件。

錨定的零件仍然可以通過修改其 CFramePosition 來移動,仍然可能具有非零 AssemblyLinearVelocityAssemblyAngularVelocity

最後,如果未錨定的零件與錨定的零件通過像 Weld 這樣的對象連接,它也會作為錨定的零件。如果此聯合破裂,零件可能再次受到物理影響。請參閱裝配以獲得更多詳情。

無法在錨定的零件上設定網路擁有權。如果零件的錨定狀態在服務伺服器上發生變化,那零件的網路擁有權將受到影響。

範例程式碼

This code sample will allow a part to be clicked to toggle its anchored property. When toggled, the visual appearance of the part is updated (red means anchored, yellow means free).

Part Anchored Toggle

local part = script.Parent
-- Create a ClickDetector so we can tell when the part is clicked
local cd = Instance.new("ClickDetector", part)
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
if part.Anchored then
-- When the part is anchored...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- When the part is unanchored...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Toggle the anchored property
part.Anchored = not part.Anchored
-- Update visual state of the brick
updateVisuals()
end
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)

AssemblyAngularVelocity

未複製
平行讀取

這個零件的裝配角速度向量。它是每秒鐘角度變化率以弧度計。

角度速度在裝配的每一點都相同。

直接設置速度可能會導致不切實際的運動。使用 TorqueAngularVelocity 限制是首選,或使用 ApplyAngularImpulse() 如果你想要立即變更速度。

如果零件是由服務伺服器擁有 (不是由 或 設置為 的服務器擁有),此屬性必須從服務器變更為 (而不是從 設置為 的服務器)。如果零件通過 自動 擁有權轉讓給客戶,此屬性可以從客戶端腳本 或服務器腳指令碼更改;從服務器端腳本更改零件擁有權將無效。

AssemblyCenterOfMass

唯讀
未複製
平行讀取

位置是通過所有零件裝配中的 MassPosition 計算出來的。

如果裝配有錨定的零件,那個零件的質量中心將是裝配的質量中心,裝配將擁有無限的質量。

了解質量中心可以幫助組裝保持穩定。對質量中心施加的力不會導致角度加速,只有線性。具有低重心的裝配會在重力效果下保持更好的時間。

AssemblyLinearVelocity

未複製
平行讀取

這個零件的裝配的線速度向量。它是每秒鐘的位置變化率 AssemblyCenterOfMass 在螺柱上。

如果您想知道速度在裝配的中心質量以外的點,請使用 GetVelocityAtPosition()

直接設置速度可能會導致不切實際的運動。使用 VectorForce 限制是首選,或使用 ApplyImpulse() 如果你想要立即變更速度。

如果零件是由服務伺服器擁有 (不是由 或 設置為 的服務器擁有),此屬性必須從服務器變更為 (而不是從 設置為 的服務器)。如果零件通過 自動 擁有權轉讓給客戶,此屬性可以從客戶端腳本 或服務器腳指令碼更改;從服務器端腳本更改零件擁有權將無效。

AssemblyMass

唯讀
未複製
平行讀取

本部分裝配中所有 BaseParts 的質量總和。不是裝配根部分的零件Massless將不會貢獻AssemblyMass

如果裝配有錨定的零件,裝配的重量被視為無限。未錨定的裝配與大量差異的物理互動限制可能會導致不穩定。

AssemblyRootPart

唯讀
未複製
平行讀取

此屬性表示將自動選擇 BasePart 來代表裝配的根部分。這是開發者呼叫 GetRootPart() 時返回的相同部分。

根部分可以通過變更裝配中零件的 RootPriority 來更改。

所有共享相同 AssemblyRootPart 的零件都在同一裝配中。

有關根部件的更多資訊,請參閱裝配

AudioCanCollide

平行讀取

AudioCanCollide 決定零件是否會物理與音響模擬互動,與燈光的 CastShadow 類似。

停用時,音頻通過零件;它不被遮蓋或反射。

BackSurface

平行讀取

BackSurface 屬性決定用於零件正面 Z 方向的表面類型。當兩個零件的面積放置在一起時,它們可能會創建一個聯結在他們之間。

BottomSurface

平行讀取

BottomSurface 屬性決定了用於零件負 Y 方向的表面類型。當兩個零件的面積放置在一起時,它們可能會創建一個聯結在他們之間。

BrickColor

未複製
平行讀取

此屬性決定零件的顏色。如果零件有 Material,這也會決定在渲染材料紋理時使用的顏色。對於更多對顏色的控制,Color屬性可以使用,這個屬性將使用最接近的BrickColor

零件的其他視覺特性由 TransparencyReflectance 決定。

CFrame

平行讀取

CFrame 屬性決定了世界中 BasePart 的位置和方向。它作為幾何上的任意參考位置,但 ExtentsCFrame 代表其物理中心的實際 CFrame

當在零件上設置 CFrame 時,其他加入的零件也會相對於零件移動,但建議您使用 PVInstance:PivotTo() 來移動整個模型,例如當傳送玩家角色時。

與設置 BasePart.Position 不同,設置 CFrame 將永遠將零件移動到指定的 CFrame;換言之: 沒有重疊檢查 ,物理解決器將嘗試解決任何重疊,除非兩個零件都是 Anchored

若要跟蹤零件的位置 CFrame ,一個 Attachment 可能有用。

範例程式碼

This code sample demonstrates setting a part's CFrame in many different ways. It showcases how to create and compose CFrame values. It references a sibling part called "OtherPart" for demonstrating relative positioning.

Setting Part CFrame

local part = script.Parent:WaitForChild("Part")
local otherPart = script.Parent:WaitForChild("OtherPart")
-- Reset the part's CFrame to (0, 0, 0) with no rotation.
-- This is sometimes called the "identity" CFrame
part.CFrame = CFrame.new()
-- Set to a specific position (X, Y, Z)
part.CFrame = CFrame.new(0, 25, 10)
-- Same as above, but use a Vector3 instead
local point = Vector3.new(0, 25, 10)
part.CFrame = CFrame.new(point)
-- Set the part's CFrame to be at one point, looking at another
local lookAtPoint = Vector3.new(0, 20, 15)
part.CFrame = CFrame.lookAt(point, lookAtPoint)
-- Rotate the part's CFrame by pi/2 radians on local X axis
part.CFrame = part.CFrame * CFrame.Angles(math.pi / 2, 0, 0)
-- Rotate the part's CFrame by 45 degrees on local Y axis
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(45), 0)
-- Rotate the part's CFrame by 180 degrees on global Z axis (note the order!)
part.CFrame = CFrame.Angles(0, 0, math.pi) * part.CFrame -- Pi radians is equal to 180 degrees
-- Composing two CFrames is done using * (the multiplication operator)
part.CFrame = CFrame.new(2, 3, 4) * CFrame.new(4, 5, 6) --> equal to CFrame.new(6, 8, 10)
-- Unlike algebraic multiplication, CFrame composition is NOT communitative: a * b is not necessarily b * a!
-- Imagine * as an ORDERED series of actions. For example, the following lines produce different CFrames:
-- 1) Slide the part 5 units on X.
-- 2) Rotate the part 45 degrees around its Y axis.
part.CFrame = CFrame.new(5, 0, 0) * CFrame.Angles(0, math.rad(45), 0)
-- 1) Rotate the part 45 degrees around its Y axis.
-- 2) Slide the part 5 units on X.
part.CFrame = CFrame.Angles(0, math.rad(45), 0) * CFrame.new(5, 0, 0)
-- There is no "CFrame division", but instead simply "doing the inverse operation".
part.CFrame = CFrame.new(4, 5, 6) * CFrame.new(4, 5, 6):Inverse() --> is equal to CFrame.new(0, 0, 0)
part.CFrame = CFrame.Angles(0, 0, math.pi) * CFrame.Angles(0, 0, math.pi):Inverse() --> equal to CFrame.Angles(0, 0, 0)
-- Position a part relative to another (in this case, put our part on top of otherPart)
part.CFrame = otherPart.CFrame * CFrame.new(0, part.Size.Y / 2 + otherPart.Size.Y / 2, 0)

CanCollide

平行讀取

CanCollide 決定零件是否會物理上與其他零件互動。停用時,其他部分可以繼續通過零件而不受中斷。用於裝飾的零件通常會被禁用,因為它們不需要由物理引擎考慮。

如果零件不是 Anchored 且有 CanCollide 被禁用,它可能會從世界中掉落,最終被 Workspace.FallenPartsDestroyHeight 摧毀。

CanCollide 被禁用時,零件仍可發射 Touched 事件(以及其他零件觸碰它們)。您可以使用 CanTouch 來禁用此功能。

有關碰撞的更多資訊,請參閱碰撞

範例程式碼

This code sample shows how a part can fade away when touched by a Humanoid then reappear a moment after to create a passable door.

Fade Door

-- Paste into a Script inside a tall part
local part = script.Parent
local OPEN_TIME = 1
-- Can the door be opened at the moment?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Black")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Bright blue")
end
local function onTouch(otherPart)
-- If the door was already open, do nothing
if debounce then
print("D")
return
end
-- Check if touched by a Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("not human")
return
end
-- Perform the door opening sequence
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()

CanQuery

平行讀取

此屬性決定零件在空間查詢操作中是否被考慮,例如 GetPartBoundsInBoxRaycast 。請注意, 必須被禁用,才能使 生效,並且空間查詢功能永遠不會包含含有 的部分。

超過此屬性,還可以使用 OverlapParamsRaycastParams 對象來排除屬於給定列表的零件的子零件,當呼叫空間查詢函數時。

CanTouch

平行讀取

此屬性決定是否 TouchedTouchEnded 事件在零件上發生。如果 , 其他接觸部分也必須設為 以啟動觸摸事件。如果 false,觸發事件無法為零件設置,嘗試這樣做會發生錯誤。相同地,如果屬性在觸發觸碰事件後設為 false,事件將被切斷,並移除 TouchTransmitter

請注意,此碰撞邏輯可以通過 碰撞群 屬性設置遵守 Workspace.TouchesUseCollisionGroups。如果 true , 非碰撞群中的零件將忽略 觸碰事件的碰撞,因此這個屬性變得無關。

性能

對於具有 CanTouchCanCollide 設為 false 的零件,存在很小的性能提升,因為這些零件永遠不需要計算任何類型的零件對零件碰撞。然而,它們仍然可以被 RaycastsOverlapParams 查詢擊中。

CastShadow

平行讀取

決定零件是否投射陰影。停用此特性對給定的零件可能會導致對該零件投射的陰影上的視覺缺陷。

此特性不設計用於性能提升,但在複雜的場景中,戰略地停用某些部分可能會提高履約。由於視覺效果的可能性,我們建議在大多數情況下將其啟用於所有零件。

CenterOfMass

唯讀
未複製
平行讀取

CenterOfMass描述了零件質量中心的 本地 位置。如果這是單一零件裝配,這是從世界空間轉換為本地的 AssemblyCenterOfMass 。在簡單的 Parts 上,質量中心總是 (0, 0, 0) ,但可能會變化為 WedgePartMeshPart

CollisionGroup

未複製
平行讀取

CollisionGroup 屬性描述零件碰撞群的名稱(最多100個字符)。零件從預設群組開始,其名稱為 "Default" 。此值不能為空。

雖然此特性本身不會複製,但引擎會內部複製值通過另一個私有屬性來解決回退兼容問題。

範例程式碼

This example demonstrates one basic use of collision groups. It assigns BallPart to "CollisionGroupBall" and DoorPart to "CollisionGroupDoor", then makes the two groups non-collidable using PhysicsService:CollisionGroupSetCollidable().

PhysicsService:RegisterCollisionGroup

local PhysicsService = game:GetService("PhysicsService")
local collisionGroupBall = "CollisionGroupBall"
local collisionGroupDoor = "CollisionGroupDoor"
-- Register collision groups
PhysicsService:RegisterCollisionGroup(collisionGroupBall)
PhysicsService:RegisterCollisionGroup(collisionGroupDoor)
-- Assign parts to collision groups
script.Parent.BallPart.CollisionGroup = collisionGroupBall
script.Parent.DoorPart.CollisionGroup = collisionGroupDoor
-- Set groups as non-collidable with each other and check the result
PhysicsService:CollisionGroupSetCollidable(collisionGroupBall, collisionGroupDoor, false)
print(PhysicsService:CollisionGroupsAreCollidable(collisionGroupBall, collisionGroupDoor)) --> false

Color

未複製
平行讀取

Color 屬性決定零件的顏色。如果零件有 Material,這也會決定在渲染材料紋理時使用的顏色。

如果此屬性已設設定,BrickColor將使用最接近此值Color的匹配項。

零件的其他視覺特性由 TransparencyReflectance 決定。

範例程式碼

這個代碼樣本會根據玩家的整個角色擁有多少生命值來上色。它根據最大生命值生成顏色,然後設置角色內對象的顏色屬性,移除任何額外對象。

角色生命體色

-- 貼到起始角色腳本內的腳本
-- 然後玩遊戲,並搞弄你角色的健康
local char = script.Parent
local human = char.Humanoid
local colorHealthy = Color3.new(0.4, 1, 0.2)
local colorUnhealthy = Color3.new(1, 0.4, 0.2)
local function setColor(color)
for _, child in pairs(char:GetChildren()) do
if child:IsA("BasePart") then
child.Color = color
while child:FindFirstChildOfClass("Decal") do
child:FindFirstChildOfClass("Decal"):Destroy()
end
elseif child:IsA("Accessory") then
child.Handle.Color = color
local mesh = child.Handle:FindFirstChildOfClass("SpecialMesh")
if mesh then
mesh.TextureId = ""
end
elseif child:IsA("Shirt") or child:IsA("Pants") then
child:Destroy()
end
end
end
local function update()
local percentage = human.Health / human.MaxHealth
-- 根據生命值百分比來透過漸變創建顏色
-- 顏色從色健康(100%)----- >色不健康(0%)
local color = Color3.new(
colorHealthy.R * percentage + colorUnhealthy.r * (1 - percentage),
colorHealthy.G * percentage + colorUnhealthy.g * (1 - percentage),
colorHealthy.B * percentage + colorUnhealthy.b * (1 - percentage)
)
setColor(color)
end
update()
human.HealthChanged:Connect(update)

CurrentPhysicalProperties

唯讀
未複製
平行讀取

CurrentPhysicalProperties 指示零件目前的物理特性。您可以為每個零件的物理特性設置自訂值、自訂材料和材料覆寫。Roblox 引擎在決定零件的有效物理特性時,會優先考慮更細粒度的定義。以下列表中的值依照優先級由最高到最低的順序:

  • 零件的特殊物理性質
  • 零件的特殊物質的特殊物理特性
  • 物料覆蓋零件物料的自訂物理特性
  • 零件材料的預設物理特性

CustomPhysicalProperties

平行讀取

CustomPhysicalProperties 讓您自定義零件的各種物理特性,例如密度、摩擦和彈性。

啟用此屬性可讓您配置這些物理屬性。如果停用,這些物理屬性由零件的 Material 決定。

範例程式碼

This code sample demonstrates how to set the CustomPhysicalProperties property of a part.

Set CustomPhysicalProperties

local part = script.Parent
-- This will make the part light and bouncy!
local DENSITY = 0.3
local FRICTION = 0.1
local ELASTICITY = 1
local FRICTION_WEIGHT = 1
local ELASTICITY_WEIGHT = 1
local physProperties = PhysicalProperties.new(DENSITY, FRICTION, ELASTICITY, FRICTION_WEIGHT, ELASTICITY_WEIGHT)
part.CustomPhysicalProperties = physProperties

EnableFluidForces

平行讀取

true 時,啟用時 Workspace.FluidForces 時,物理引擎會在此 BasePart 上計算空力力量。

ExtentsCFrame

唯讀
未複製
平行讀取

代表物理極限 BasePartCFrame ,代表其物理中心。

ExtentsSize

唯讀
未複製
平行讀取

物理引擎對 BasePart 的實際物理尺寸,例如在 碰撞偵測 中。

FrontSurface

平行讀取

FrontSurface 屬性決定了用於零 Z 方向的零件的表面類型。當兩個零件的面積放置在一起時,它們可能會創建一個聯結在他們之間。

LeftSurface

平行讀取

LeftSurface 屬性決定用於零件負 X 方向的表面類型。當兩個零件的面積放置在一起時,它們可能會創建一個聯結在他們之間。

LocalTransparencyModifier

隱藏
未複製
平行讀取

LocalTransparencyModifier屬性是一個乘數到Transparency,只對本地客戶可見。它不從客戶端複製到服務器,對於當零件不應為特定客戶端渲染時,有用,例如當玩家縮放到第一人稱模式時,他們不能看到角色身體部位。

此屬性通過以下方程式修改本地部分的透明度,結果值位於 01 之間。

1 - (( 1 - Transparency ) × ( 1 - LocalTransparencyModifier ))


<th><code>本地透明修改器</code></th>
<th>服務器側</th>
<th>客戶端</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>0.5</code></td>
<td><code>0</code></td>
<td><code>0.5</code></td>
<td><code>0.5</code></td>
</tr>
<tr>
<td><code>0.5</code></td>
<td><code>0.25</code></td>
<td><code>0.5</code></td>
<td><code>0.625</code></td>
</tr>
<tr>
<td><code>0.5</code></td>
<td><code>0.5</code></td>
<td><code>0.5</code></td>
<td><code>0.75</code></td>
</tr>
<tr>
<td><code>0.5</code></td>
<td><code>0.75</code></td>
<td><code>0.5</code></td>
<td><code>0.875</code></td>
</tr>
<tr>
<td><code>0.5</code></td>
<td><code>1</code></td>
<td><code>0.5</code></td>
<td><code>1</code></td>
</tr>
</tbody>

Locked

平行讀取

屬性決定是否可以在 Studio 中通過單擊選擇零件(或其包含的零件)。此特性通常會在環境模型中的零件中啟用,這些零件目前未在編輯中。

範例程式碼

This code sample uses the concept of recursion to unlock all parts that are a descendant of a model.

Recursive Unlock

-- Paste into a Script within a Model you want to unlock
local model = script.Parent
-- This function recurses through a model's heirarchy and unlocks
-- every part that it encounters.
local function recursiveUnlock(object)
if object:IsA("BasePart") then
object.Locked = false
end
-- Call the same function on the children of the object
-- The recursive process stops if an object has no children
for _, child in pairs(object:GetChildren()) do
recursiveUnlock(child)
end
end
recursiveUnlock(model)

Mass

唯讀
未複製
平行讀取

Mass 是一個只讀屬性,用於描述零件的體積和密度的產品。它由 GetMass() 函數返回。

Massless

平行讀取

如果此特性啟用,零件將不會對組裝的總質量或慣性做出貢獻,只要它與另一個有質量的零件焊接在一起。

如果零件是自己的根部分,根據 AssemblyRootPart ,這將被忽略為該零件,仍會為其裝配提供質量和慣性貢獻,就像普通零件一樣。沒有物質的零件永遠不應成為裝配根部分,除非所有其他零件也沒有物質。

這可能對於像選擇性配件在車輛上不想影響車輛操控或沒有質量的渲染網格焊接到簡單的碰撞網格有用。

也可以參閱裝配文章,該文件記錄了根部件是什麼以及如何使用它們。

平行讀取

Material 屬性允許您設置零件的紋理和預設物理屬性(如果 CustomPhysicalProperties 未設置)。預設Plastic材料的紋理非常淺,而SmoothPlastic材料則完全部 所有沒有紋理。一些材料紋理,例如 DiamondPlateGranite 有非常明顯的紋理。每種材料的紋理反射出不同的陽光,特別是Foil

設定此屬性,然後啟用 CustomPhysicalProperties 將使用材料的預設物理屬性。例個體、實例, DiamondPlate 是非常稠密的材料,而 Wood 是非常輕。零件的密度決定它是否會在地形水中漂浮。

在中等圖形設定下,材料變更渲染行為通過應用一點反射率(類似於 )和扭曲扭曲來改變圖形效果。效果特別明顯在圓形零件上。半透明零件背後的 Glass 零件不可見。

MaterialVariant

未複製
平行讀取

系統搜尋指定的 MaterialVariant 名稱和 MaterialVariant 輸入的 Material 實例。如果成功找到匹配的 MaterialVariant 個體、實例,它會使用該實例來替換預設材料。預設材料可以是內置材料或覆蓋 MaterialVariantMaterialService 中指定的材料。

Orientation

隱藏
未複製
平行讀取

Orientation描述了零件在 XYZ 軸上的旋轉度,使用Vector3來進行。旋轉在 Y > X > Z 順序中應用。這與正確的 尤里 角度不同,而是 泰特-布萊恩 角度,用於描述 偏航 , 傾斜滾動

值得注意的是,這個屬性與在不同順序中應用旋轉的 CFrame.Angles() 建造器( Z > Y > X )有何不同。為了更好地控制零件的旋轉,建議設置 CFrame 而不是。

當設置此屬性時,任何WeldsMotor6Ds連接到此部分的C0C1屬性將被更新,以允許部分相對於任何其他部分移動。WeldConstraints 在移動工具動期間也會暫時停用並重新啟用。

範例程式碼

這個代碼範例會持續在 Y 軸上旋轉零件。

零件旋轉器

local part = script.Parent
local INCREMENT = 360 / 20
-- 持續旋轉零件
while true do
for degrees = 0, 360, INCREMENT do
-- 只設定 Y 軸旋轉
part.Rotation = Vector3.new(0, degrees, 0)
-- 一個更好的方法是設置 CFrame
--部分.CFrame = CFrame.新(部分.位置) * CFrame.角度(0, math.rad(度), 0)
task.wait()
end
end

PivotOffset

平行讀取

此屬性指定零件的旋轉點與其 CFrame 的偏移值,即 BasePart:GetPivot()BasePart.CFrame 乘以 BasePart.PivotOffset 相同。

將旋轉點設置為 本地 空間的位置很方便,但將零件的旋轉點設置為 世界 空間的位置可以按照以下方式進行:


local Workspace = game:GetService("Workspace")
local part = Workspace.BluePart
local desiredPivotCFrameInWorldSpace = CFrame.new(0, 10, 0)
part.PivotOffset = part.CFrame:ToObjectSpace(desiredPivotCFrameInWorldSpace)

範例程式碼

This code sample shows a custom function for resetting the pivot of a model back to the center of that model's bounding box.

Reset Pivot

local function resetPivot(model)
local boundsCFrame = model:GetBoundingBox()
if model.PrimaryPart then
model.PrimaryPart.PivotOffset = model.PrimaryPart.CFrame:ToObjectSpace(boundsCFrame)
else
model.WorldPivot = boundsCFrame
end
end
resetPivot(script.Parent)

這個代碼示例在原點創建一個時鐘,使用分、秒和小時手搖擺,並使其發出聲響,顯示本地時間。

時鐘手

local function createHand(length, width, yOffset)
local part = Instance.new("Part")
part.Size = Vector3.new(width, 0.1, length)
part.Material = Enum.Material.Neon
part.PivotOffset = CFrame.new(0, -(yOffset + 0.1), length / 2)
part.Anchored = true
part.Parent = workspace
return part
end
local function positionHand(hand, fraction)
hand:PivotTo(CFrame.fromEulerAnglesXYZ(0, -fraction * 2 * math.pi, 0))
end
-- 創建呼叫
for i = 0, 11 do
local dialPart = Instance.new("Part")
dialPart.Size = Vector3.new(0.2, 0.2, 1)
dialPart.TopSurface = Enum.SurfaceType.Smooth
if i == 0 then
dialPart.Size = Vector3.new(0.2, 0.2, 2)
dialPart.Color = Color3.new(1, 0, 0)
end
dialPart.PivotOffset = CFrame.new(0, -0.1, 10.5)
dialPart.Anchored = true
dialPart:PivotTo(CFrame.fromEulerAnglesXYZ(0, (i / 12) * 2 * math.pi, 0))
dialPart.Parent = workspace
end
-- 創建手
local hourHand = createHand(7, 1, 0)
local minuteHand = createHand(10, 0.6, 0.1)
local secondHand = createHand(11, 0.2, 0.2)
-- 運行時間表
while true do
local components = os.date("*t")
positionHand(hourHand, (components.hour + components.min / 60) / 12)
positionHand(minuteHand, (components.min + components.sec / 60) / 60)
positionHand(secondHand, components.sec / 60)
task.wait()
end

Position

隱藏
未複製
平行讀取

Position 屬性描述使用 Vector3 來描述零件的坐標。它反映了零件的CFrame位置,但也可以設設定。

當設置此屬性時,任何WeldsMotor6Ds連接到此部分的C0C1屬性將被更新,以允許部分相對於任何其他部分移動。WeldConstraints 在移動工具動期間也會暫時停用並重新啟用。

ReceiveAge

隱藏
唯讀
未複製
平行讀取

指示零件的物理最後更新於本地客戶端或伺服器的時間(以秒為單位)。當零件沒有物理時(Anchoredtrue),此值將為0

Reflectance

平行讀取

Reflectance 屬性決定零件反映天空的程度。值 0 表示零件不反射任全部 所有東西,值 1 表示零件完全反射。

反射不受 Transparency 影響,除非零件完全透明,否則反射將不全部 所有渲染。反射率可能會或可能不會被忽略,取決於零件的 Material

ResizeIncrement

唯讀
未複製
平行讀取

ResizeIncrement 屬性是一個只讀屬性,用於描述 Resize() 方法允許的最小變更尺寸。它與 BasePart 抽象類別的實現有所不同;例個體、實例, Part 設置為 1 ,因為個別桁架部分的尺寸為 2×2×2;而 TrussPart 設置為 2 ,因為個別桁架部分的尺寸為 2×2×2。

ResizeableFaces

唯讀
未複製
平行讀取

ResizeableFaces 屬性使用 Faces 對象來描述可以縮放零件的不同面。對於大多數 BasePart 的實裝,例如 PartWedgePart,此屬性包括所有面。然而, 將將其 設置設置為僅兩個面,因為這種類型的零件必須有兩個 長度尺寸。

此屬性最常用於建造和操縱零件的工具,在那之外的情況下使用量很少。具有 Handles 屬性的 Handles.Faces 可以與此屬性結合,以顯示只有在零件上可以調整尺寸的面上的手柄。

範例程式碼

This code sample creates a Handles object and shows how to set the Faces property of the object. It also references ResizeableFaces of a part. Try placing this script in multiple kinds of parts to see how ResizeableFaces varies.

Resize Handles

-- Put this Script in several kinds of BasePart, like
-- Part, TrussPart, WedgePart, CornerWedgePart, etc.
local part = script.Parent
-- Create a handles object for this part
local handles = Instance.new("Handles")
handles.Adornee = part
handles.Parent = part
-- Manually specify the faces applicable for this handle
handles.Faces = Faces.new(Enum.NormalId.Top, Enum.NormalId.Front, Enum.NormalId.Left)
-- Alternatively, use the faces on which the part can be resized.
-- If part is a TrussPart with only two Size dimensions
-- of length 2, then ResizeableFaces will only have two
-- enabled faces. For other parts, all faces will be enabled.
handles.Faces = part.ResizeableFaces

RightSurface

平行讀取

RightSurface 屬性決定了用於零件正面 X 方向的表面類型。當兩個零件的面積放置在一起時,它們可能會創建一個聯結在他們之間。

RootPriority

平行讀取

此屬性是在 -127127 之間的整數,優先於所有其他規則對根部分排序。當考慮不是 Anchored 且共享相同 Massless 值的多個零件時,具有更高 RootPriority 值的零件將取代那些具有更低 RootPriority 值的零件。

您可以使用此屬性來控制裝配中哪一部分是根部分,並在尺寸變更時保持根部分穩定。

也可以參閱裝配文章,該文件記錄了根部件是什麼以及如何使用它們。

Rotation

未複製
平行讀取

零件在三軸上的旋轉度。

當設置此屬性時,任何WeldsMotor6Ds連接到此部分的C0C1屬性將被更新,以允許部分相對於任何其他部分移動。WeldConstraints 在移動工具動期間也會暫時停用並重新啟用。

未複製
平行讀取

零件的 Size 屬性決定其 視覺 尺寸,而 ExtentsSize 代表物理引擎使用的實際尺寸,例如在 碰撞偵測 中。個別尺寸(長度、寬度、高度)可低至 0.001 和高至 2048 。尺寸低於 下的尺寸將以 視覺方式 來顯示,如果零件的尺寸是 。

零件的 Size 被用於各種額外方式:

範例程式碼

This code sample constructs a pyramid by stacking parts that get progressively smaller. It also colors the parts so they blend between a start color and end color.

Pyramid Builder

local TOWER_BASE_SIZE = 30
local position = Vector3.new(50, 50, 50)
local hue = math.random()
local color0 = Color3.fromHSV(hue, 1, 1)
local color1 = Color3.fromHSV((hue + 0.35) % 1, 1, 1)
local model = Instance.new("Model")
model.Name = "Tower"
for i = TOWER_BASE_SIZE, 1, -2 do
local part = Instance.new("Part")
part.Size = Vector3.new(i, 2, i)
part.Position = position
part.Anchored = true
part.Parent = model
-- Tween from color0 and color1
local perc = i / TOWER_BASE_SIZE
part.Color = Color3.new(
color0.R * perc + color1.R * (1 - perc),
color0.G * perc + color1.G * (1 - perc),
color0.B * perc + color1.B * (1 - perc)
)
position = position + Vector3.new(0, part.Size.Y, 0)
end
model.Parent = workspace
平行讀取

TopSurface 屬性決定了用於零件正面 Y 方向的表面類型。當兩個零件的面積放置在一起時,它們可能會創建一個聯結在他們之間。

Transparency

平行讀取

Transparency 屬性控制零件在比例為 01 的可見度,其中 0 完全可見 (不透明),而 1 完全不可見 (全全部 所有未渲染)。

雖然完全透明的零件不會被渲染,但部分透明的對象有一些重要的渲染費用。擁有許多半透明零件可能會影響履約。

透明零件重疊時,渲染順序可能會無法預測地運作,因此您應避免半透明零件重疊。

也可以看到 LocalTransparencyModifier 作為乘數到 Transparency ,只對本地客戶可見。

範例程式碼

This code sample shows how a part can fade away when touched by a Humanoid then reappear a moment after to create a passable door.

Fade Door

-- Paste into a Script inside a tall part
local part = script.Parent
local OPEN_TIME = 1
-- Can the door be opened at the moment?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Black")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Bright blue")
end
local function onTouch(otherPart)
-- If the door was already open, do nothing
if debounce then
print("D")
return
end
-- Check if touched by a Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("not human")
return
end
-- Perform the door opening sequence
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()

方法

AngularAccelerationToTorque

參數

angAcceleration: Vector3
預設值:""
angVelocity: Vector3
預設值:"0, 0, 0"

返回

ApplyAngularImpulse

()

對此零件的裝配應用即時角力脈沖,使裝配旋轉。

由脈衝產生的角速度取決於裝配的 mass 。因此需要更高的脉冲來移動更大的裝配。脈衝對於需要立即施加力的情況,例如爆炸或碰撞,很有用。

如果零件是由伺服器務器擁有 (不是由 或 設置為 的服務器呼叫此函數),則必須從服務器 呼叫此函數 (而不是從 設置為 的服務器)。如果零件通過 自動擁有 獲得擁有權,這個功能可以從客戶端腳本 或服務器指令碼本中呼叫;從客戶端腳本呼叫服務器擁有的零件將無效。

參數

impulse: Vector3

應用於裝配的角度脈衝向量。

預設值:""

返回

()

ApplyImpulse

()

此功能會對此零件的裝配應用瞬間力脈沖。

力量在裝配的 center of mass 應用,因此得到的運動只會是線性的。

由脈衝產生的速度取決於裝配的 mass 。因此需要更高的脉冲來移動更大的裝配。脈衝對於需要立即施加力的情況,例如爆炸或碰撞,很有用。

如果零件是由伺服器務器擁有 (不是由 或 設置為 的服務器呼叫此函數),則必須從服務器 呼叫此函數 (而不是從 設置為 的服務器)。如果零件通過 自動擁有 獲得擁有權,這個功能可以從客戶端腳本 或服務器指令碼本中呼叫;從客戶端腳本呼叫服務器擁有的零件將無效。

參數

impulse: Vector3

應用於裝配的線性脈衝向量。

預設值:""

返回

()

ApplyImpulseAtPosition

()

此功能會在世界空間的指定位置對此零件的裝配施加瞬間力脈沖。

如果位置不在裝配的 center of mass ,脈沖將導致位置和旋轉運動。

由脈衝產生的速度取決於裝配的 mass 。因此需要更高的脉冲來移動更大的裝配。脈衝對於想要立即施加力的情況,例如爆炸或碰撞,很有用。

如果零件是由伺服器務器擁有 (不是由 或 設置為 的服務器呼叫此函數),則必須從服務器 呼叫此函數 (而不是從 設置為 的服務器)。如果零件通過 自動擁有 獲得擁有權,這個功能可以從客戶端腳本 或服務器指令碼本中呼叫;從客戶端腳本呼叫服務器擁有的零件將無效。

參數

impulse: Vector3

應用於裝配的脈衝向量。

預設值:""
position: Vector3

在世界空間中的位置,以應用脈衝。

預設值:""

返回

()

CanCollideWith

平行寫入

返回零件是否可以相互碰撞。此功能會考慮兩個零件的碰撞群。如果指定的零件不是基本零件,此功能將發生錯誤。

參數

part: BasePart

指定的零件正在檢查是否存在碰撞。

預設值:""

返回

零件是否能相互碰撞。

CanSetNetworkOwnership

CanSetNetworkOwnership 函數檢查您是否可以設置零件的網絡所有權。

函數的返回值檢查您是否可以無遇到錯誤地呼叫 BasePart:SetNetworkOwner()BasePart:SetNetworkOwnershipAuto() 。如果您可以修改/閱讀網絡所有權,它會返回真值;如果您無法修改/閱讀網絡所有權,它會返回 false,並將理由作為字串。


返回

無論您是否可以修改或閱讀網絡所有權和原因。

範例程式碼

This example checks whether or not the network ownership of the first BasePart named Part in the Workspace can be set.

Check if a Part's Network Ownership Can Be Set

local part = workspace:FindFirstChild("Part")
if part and part:IsA("BasePart") then
local canSet, errorReason = part:CanSetNetworkOwnership()
if canSet then
print(part:GetFullName() .. "'s Network Ownership can be changed!")
else
warn("Cannot change the Network Ownership of " .. part:GetFullName() .. " because: " .. errorReason)
end
end

GetClosestPointOnSurface

參數

position: Vector3
預設值:""

返回

GetConnectedParts

Instances
平行寫入

返回包含任何種類剛性聯組合的對象與零件的表。

如果 recursive 是真的,這個函數將返回裝配中所有與基礎零件緊密連接的零件。

剛性聯節

當關節連接兩個零件一起時 (Part0 → Part1) , 關節是 剛性 如果物理學的 Part1Part0 完全鎖定。這只適用於以下聯合類型:

參數

recursive: boolean

一個由任何種類的 joint 連接到對象的零件表。

預設值:false

返回

Instances

GetJoints

Instances
平行寫入

返回連接到此零件的所有關節或限制。


返回

Instances

與零件連接的所有聯節或限制的一個數組。

GetMass

平行寫入

取得量 返回閱讀僅限的 Mass 屬性的值。

此功能早於「大量」屬性。它仍然支持退款兼容;你應該直接使用「大量」屬性。


返回

零件的重量。

範例程式碼

這個例子在遊戲的工作區創建了一個新零件,myPart,其尺寸為 4x6x4 格。零件也被錨定。

然後,myMass設為新零件的質量相等。零件的質量在印刷聲明結束時打印:

我的零件的重量是...

尋找零件的重量

local myPart = Instance.new("Part")
myPart.Size = Vector3.new(4, 6, 4)
myPart.Anchored = true
myPart.Parent = workspace
local myMass = myPart:GetMass()
print("My part's mass is " .. myMass)

GetNetworkOwner

平行寫入

返回這部分網絡所有者的當前玩家,或在伺服器的情況下返回 nil


返回

當前是這部分網絡擁有者的玩家,或者 nil 在伺服器的情況下。

GetNetworkOwnershipAuto

平行寫入

如果遊戲引擎自動決定此部分的網絡所有者,返回真值。


返回

遊戲引擎是否自動決定網絡所有者對於此部分。

GetNoCollisionConstraints

Instances

返回

Instances

GetRootPart

平行寫入

返回裝配的基本部分。當使用 CFrame 移動零件組合時。移動此基礎零件(這將適當移動所有與它連接的其他零件)很重要。更多資訊可在 裝配 文章中找到。

此功能早於 AssemblyRootPart 屬性。它仍然支持退款,但您應該直接使用 AssemblyRootPart


返回

裝配的基本部分(一個連接在一起的零件集)。

GetTouchingParts

Instances

返回與此部分物理互動的所有零件的表。如果零件本身設置為「無法碰撞」為 false,則此函數將返回空表,除非零件有一個 TouchInterest 對象連接到它 (即意味著有東西連接到其觸碰事件)。相鄰但不相交的零件不會被視為接觸。此功能優先於 WorldRoot:GetPartsInPart() 功能,提供更多彈性並避免上述特殊 TouchInterest 規則所述的問題。使用 WorldRoot:GetPartsInPart() 取代。


返回

Instances

一個包含所有交叉部件,可能與此部件碰撞的表。

GetVelocityAtPosition

平行寫入

返回零件在指定位置相對於此零件的線速度。它可用於識別與根部零件不同的裝配中零件的線速度。如果裝配沒有角速度,則線速度將永遠一致於每個位置。

參數

position: Vector3
預設值:""

返回

IsGrounded

平行寫入

如果對象連接到會將其保持在位置的零件(例如 Anchored 零件),則返回真值;否則返回假值。在裝配中包含 Anchored 零件的情況下,其他每個零件都被接地。


返回

物件是否連接到會將其保持在空間的零件。

Resize

以相同於使用 Studio 縮放工具的方式變更對象的大小。

參數

normalId: Enum.NormalId

要縮調整大小的側面。

預設值:""
deltaAmount: number

在指定的一側增長/縮小多少。

預設值:""

返回

零件是否已縮放。

SetNetworkOwner

()

將指定的玩家設為網絡所有者,用於此網絡和所有連接的零件。當玩家Instance是 nil 時,伺服器將成為玩家而不是玩家的所有者。

參數

playerInstance: Player

給予玩家擁有零件網絡所有權。

預設值:"nil"

返回

()

SetNetworkOwnershipAuto

()

讓遊戲引擎動態決定誰會處理零件的物理(客戶端之一或服務伺服器)。


返回

()

TorqueToAngularAcceleration

參數

torque: Vector3
預設值:""
angVelocity: Vector3
預設值:"0, 0, 0"

返回

IntersectAsync

暫停

從零件和給定數陣列中的其他零件的交集幾何中創建一個新的 IntersectOperation 。只支持 Parts ,不支持 TerrainMeshParts 。與 Clone() 類似,返回的對象沒有設置 Parent

呼叫方的下列屬性將適用於結果的 IntersectOperation :

在下面的圖像比較中,IntersectAsync() 被稱為使用包含藍色方塊的表來調用紫色方磚塊。最終得到的 IntersectOperation 會轉化為兩個零件的交集幾何形狀。

Two block parts overlapping

<figcaption>分開零件</figcaption>
Parts intersected into a new solid model

<figcaption>結果 <code>Class.IntersectOperation</code></figcaption>

注意事項

  • 原始零件在成功的交叉操作後保持完好。在大多數情況下,您應該 Destroy() 所有原始零件,並將返回的 IntersectOperation 返回到與呼叫 BasePart 相同的地方。
  • 預設情況下,結果交叉點的面顏色會從原始零件的 Color 屬性中借用。若要將整個交叉點變更為特定顏色,請將其 UsePartColor 屬性設為 true
  • 如果交叉操作會導致超過 20,000 個三角形的零件,將簡化為 20,000 個三角形。

參數

parts: Instances

參與交叉的對象。

預設值:""
collisionfidelity: Enum.CollisionFidelity
預設值:"Default"
renderFidelity: Enum.RenderFidelity
預設值:"Automatic"

返回

結果 IntersectOperation 使用預設名稱 交叉

SubtractAsync

暫停

從零件中創建一個新的 UnionOperation ,減去給定數陣列中零件所佔的幾何圖形。只支持 Parts ,不支持 TerrainMeshParts 。與 Clone() 類似,返回的對象沒有設置 Parent

請注意,由於減法造成的結果聯盟不能為空,如果操作會導致完全空白的幾何圖形,它將失敗。

在下面的圖像比較中,SubtractAsync() 被稱為使用包含紫色方磚塊的表在藍色圓筒上呼叫。最終得到的 UnionOperation 形狀會將方磚塊的幾何從圓筒的幾何中刪除。

Longer block overlapping a cylinder

<figcaption>分開零件</figcaption>
Block part subtracted from cylinder

<figcaption>結果 <code>Class.UnionOperation</code></figcaption>

參數

parts: Instances

參與減除的對象。

預設值:""
collisionfidelity: Enum.CollisionFidelity
預設值:"Default"
renderFidelity: Enum.RenderFidelity
預設值:"Automatic"

返回

結果 UnionOperation 使用預設名稱 聯盟

範例程式碼

這個例子顯示如何從另一個 BasePart 中扣除部分來形成否定的 UnionOperation

基本零件:減除异步()

local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- 執行減法操作
local success, newSubtract = pcall(function()
return mainPart:SubtractAsync(otherParts)
end)
-- 如果操作成功,將它放置在同一位置並將其作為工作區的父級
if success and newSubtract then
newSubtract.Position = mainPart.Position
newSubtract.Parent = Workspace
end
-- 摧毀操作後仍然完好的原始零件
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
end

UnionAsync

暫停

從零件中創建一個新的 UnionOperation ,加上給定數陣列中零件所佔的幾何圖形。只支持 Parts ,不支持 TerrainMeshParts 。與 Clone() 類似,返回的對象沒有設置 Parent

呼叫方的下列屬性將適用於結果的 UnionOperation :

在下面的圖像比較中,UnionAsync() 被稱為使用紫色圓筒的藍色方塊使用表中的表。最終得到的 UnionOperation 會轉化為兩個零件的組合幾何形狀。

Block and cylinder parts overlapping

<figcaption>分開零件</figcaption>
Parts joined together into a single solid union

<figcaption>結果 <code>Class.UnionOperation</code></figcaption>

注意事項

  • 原始零件在成功的聯合操作後保持完好。在大多數情況下,您應該 Destroy() 所有原始零件,並將返回的 UnionOperation 返回到與呼叫 BasePart 相同的地方。
  • 預設情況下,結果聯盟會尊重每個零件的 Color 屬性。若要將整個聯盟變更為特定顏色,請將其 UsePartColor 屬性設為 true
  • 如果聯合操作會導致超過 20,000 個三角形的部分,將簡化為 20,000 個三角形。

參數

parts: Instances

參與與呼叫部分結合的對象。

預設值:""
collisionfidelity: Enum.CollisionFidelity
預設值:"Default"
renderFidelity: Enum.RenderFidelity
預設值:"Automatic"

返回

結果 UnionOperation 使用預設名稱 聯盟

範例程式碼

This example demonstrates how to combine the geometry of one BasePart with the geometry of other part(s) to form a UnionOperation.

BasePart:UnionAsync()

local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Perform union operation
local success, newUnion = pcall(function()
return mainPart:UnionAsync(otherParts)
end)
-- If operation succeeds, position it at the same location and parent it to the workspace
if success and newUnion then
newUnion.Position = mainPart.Position
newUnion.Parent = Workspace
end
-- Destroy original parts which remain intact after operation
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
end

活動

TouchEnded

在相似 BasePart.Touched 條件下停止接觸其他部件時發生火災。

這個事件與 Workspace.TouchesUseCollisionGroups 一起工作,以確定是否認可衝突群進行偵測。

參數

otherPart: BasePart

範例程式碼

This code sample creates a BillboardGui on a part that displays the number of parts presently touching it.

Touching Parts Count

local part = script.Parent
local billboardGui = Instance.new("BillboardGui")
billboardGui.Size = UDim2.new(0, 200, 0, 50)
billboardGui.Adornee = part
billboardGui.AlwaysOnTop = true
billboardGui.Parent = part
local tl = Instance.new("TextLabel")
tl.Size = UDim2.new(1, 0, 1, 0)
tl.BackgroundTransparency = 1
tl.Parent = billboardGui
local numTouchingParts = 0
local function onTouch(otherPart)
print("Touch started: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Touch ended: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)

Touched

觸碰事件會在一個零件與另一個零件接觸時發生。例個體、實例,如果 零件A 撞到 零件B ,則 PartA.Touched 使用 零件B 發射,而 PartB.Touched 使用 零件A 發射。

此事件只會在物理移動的結果發生,因此如果 CFrame 屬性被變更,使零件與另一零件重疊,就不會發生。這也意味著至少一個涉及的零件必須 在碰撞時 Anchored

這個事件與 Workspace.TouchesUseCollisionGroups 一起工作,以確定是否認可衝突群進行偵測。

參數

otherPart: BasePart

與指定部分接觸的另一部分。


範例程式碼

This code sample creates a BillboardGui on a part that displays the number of parts presently touching it.

Touching Parts Count

local part = script.Parent
local billboardGui = Instance.new("BillboardGui")
billboardGui.Size = UDim2.new(0, 200, 0, 50)
billboardGui.Adornee = part
billboardGui.AlwaysOnTop = true
billboardGui.Parent = part
local tl = Instance.new("TextLabel")
tl.Size = UDim2.new(1, 0, 1, 0)
tl.BackgroundTransparency = 1
tl.Parent = billboardGui
local numTouchingParts = 0
local function onTouch(otherPart)
print("Touch started: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Touch ended: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)

This code sample demonstrates how to connect the BasePart.Touched event of multiple parts in a Model to one function.

Model Touched

local model = script.Parent
local function onTouched(otherPart)
-- Ignore instances of the model coming in contact with itself
if otherPart:IsDescendantOf(model) then
return
end
print(model.Name .. " collided with " .. otherPart.Name)
end
for _, child in pairs(model:GetChildren()) do
if child:IsA("BasePart") then
child.Touched:Connect(onTouched)
end
end