Kelas Engine
BasePart
*Konten ini diterjemahkan menggunakan AI (Beta) dan mungkin mengandung kesalahan. Untuk melihat halaman ini dalam bahasa Inggris, klik di sini.
Rangkuman
Properti
Metode
Acara
StoppedTouching(otherPart: BasePart):RBXScriptSignal |
Touched(otherPart: BasePart):RBXScriptSignal |
TouchEnded(otherPart: BasePart):RBXScriptSignal |
Anggota yang Diwariskan
4 yang diwarisi dari PVInstance
57 yang diwarisi dari Instance
6 yang diwarisi dari Object
Diwariskan oleh
Contoh Kode
Penghalusan CFrame dari Sebuah Bagian
local TweenService = game:GetService("TweenService")
-- buat sebuah bagian dan target
local part = Instance.new("Part", workspace)
part.Size = Vector3.one
part.Anchored = true
part.BrickColor = BrickColor.new("Birus elektrik")
part.CFrame = CFrame.new(0, 4, 0)
local target = Instance.new("Part", workspace)
target.Name = "Target"
target.Size = Vector3.one * 0.5
target.Anchored = true
target.BrickColor = BrickColor.new("Merah benar")
target.CFrame = CFrame.new(0, 8, 0)
-- siapkan sebuah variabel untuk menyimpan kecepatan
local velocity = CFrame.new()
-- sesuaikan kecepatan penghalusan
local smoothTime = 0.5
game:GetService("RunService").Stepped:Connect(function(t, dt)
part.CFrame, velocity = TweenService:SmoothDamp(part.CFrame, target.CFrame, velocity, smoothTime, math.huge, dt)
end)Referensi API
Properti
Anchored
Contoh Kode
Toggle Bagian Terikat
local part = script.Parent
-- Buat ClickDetector sehingga kita dapat mengetahui kapan bagian di-klik
local cd = Instance.new("ClickDetector", part)
-- Fungsi ini memperbarui bagaimana bagian terlihat berdasarkan status Terikatnya
local function updateVisuals()
if part.Anchored then
-- Ketika bagian terikat...
part.BrickColor = BrickColor.new("Bright red")
part.Material = Enum.Material.DiamondPlate
else
-- Ketika bagian tidak terikat...
part.BrickColor = BrickColor.new("Bright yellow")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Ubah properti terikat
part.Anchored = not part.Anchored
-- Perbarui status visual batu bata
updateVisuals()
end
-- Perbarui, lalu mulai mendengarkan klik
updateVisuals()
cd.MouseClick:Connect(onToggle)BackParamA
BackParamB
BackSurfaceInput
BottomParamA
BottomParamB
BottomSurfaceInput
brickColor
CanCollide
Contoh Kode
Pintu Memudar
-- Tempelkan di dalam Skrip di dalam bagian yang tinggi
local part = script.Parent
local OPEN_TIME = 1
-- Apakah pintu dapat dibuka saat ini?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Hitam")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Biru terang")
end
local function onTouch(otherPart)
-- Jika pintu sudah terbuka, tidak ada tindakan yang diambil
if debounce then
print("D")
return
end
-- Periksa jika disentuh oleh Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("bukan manusia")
return
end
-- Lakukan urutan pembukaan pintu
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()CFrame
Contoh Kode
Mengatur CFrame Bagian
local part = script.Parent:WaitForChild("Part")
local otherPart = script.Parent:WaitForChild("OtherPart")
-- Atur ulang CFrame bagian ke (0, 0, 0) tanpa rotasi.
-- Ini terkadang disebut sebagai CFrame "identitas"
part.CFrame = CFrame.new()
-- Atur ke posisi tertentu (X, Y, Z)
part.CFrame = CFrame.new(0, 25, 10)
-- Sama seperti di atas, tetapi gunakan Vector3 sebagai gantinya
local point = Vector3.new(0, 25, 10)
part.CFrame = CFrame.new(point)
-- Atur CFrame bagian agar berada di satu titik, melihat ke titik lain
local lookAtPoint = Vector3.new(0, 20, 15)
part.CFrame = CFrame.lookAt(point, lookAtPoint)
-- Putar CFrame bagian sebesar pi/2 radian pada sumbu X lokal
part.CFrame = part.CFrame * CFrame.Angles(math.pi / 2, 0, 0)
-- Putar CFrame bagian sebesar 45 derajat pada sumbu Y lokal
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(45), 0)
-- Putar CFrame bagian sebesar 180 derajat pada sumbu Z global (perhatikan urutannya!)
part.CFrame = CFrame.Angles(0, 0, math.pi) * part.CFrame -- Pi radian setara dengan 180 derajat
-- Menggabungkan dua CFrame dilakukan dengan menggunakan * (operator perkalian)
part.CFrame = CFrame.new(2, 3, 4) * CFrame.new(4, 5, 6) --> setara dengan CFrame.new(6, 8, 10)
-- Tidak seperti perkalian aljabar, komposisi CFrame TIDAK komutatif: a * b tidak selalu b * a!
-- Bayangkan * sebagai serangkaian tindakan yang TERURUT. Misalnya, baris berikut menghasilkan CFrame yang berbeda:
-- 1) Geser bagian 5 unit pada X.
-- 2) Putar bagian 45 derajat di sekitar sumbu Y-nya.
part.CFrame = CFrame.new(5, 0, 0) * CFrame.Angles(0, math.rad(45), 0)
-- 1) Putar bagian 45 derajat di sekitar sumbu Y-nya.
-- 2) Geser bagian 5 unit pada X.
part.CFrame = CFrame.Angles(0, math.rad(45), 0) * CFrame.new(5, 0, 0)
-- Tidak ada "pembagian CFrame", tetapi cukup "melakukan operasi invers".
part.CFrame = CFrame.new(4, 5, 6) * CFrame.new(4, 5, 6):Inverse() --> setara dengan CFrame.new(0, 0, 0)
part.CFrame = CFrame.Angles(0, 0, math.pi) * CFrame.Angles(0, 0, math.pi):Inverse() --> setara dengan CFrame.Angles(0, 0, 0)
-- Posisi satu bagian relatif terhadap bagian lain (dalam hal ini, letakkan bagian kita di atas otherPart)
part.CFrame = otherPart.CFrame * CFrame.new(0, part.Size.Y / 2 + otherPart.Size.Y / 2, 0)CollisionGroup
Contoh Kode
PhysicsService:RegisterCollisionGroup
local PhysicsService = game:GetService("PhysicsService")
local collisionGroupBall = "CollisionGroupBall"
local collisionGroupDoor = "CollisionGroupDoor"
-- Daftarkan grup tabrakan
PhysicsService:RegisterCollisionGroup(collisionGroupBall)
PhysicsService:RegisterCollisionGroup(collisionGroupDoor)
-- Tetapkan bagian ke grup tabrakan
script.Parent.BallPart.CollisionGroup = collisionGroupBall
script.Parent.DoorPart.CollisionGroup = collisionGroupDoor
-- Set grup sebagai tidak dapat bertabrakan satu sama lain dan periksa hasilnya
PhysicsService:CollisionGroupSetCollidable(collisionGroupBall, collisionGroupDoor, false)
print(PhysicsService:CollisionGroupsAreCollidable(collisionGroupBall, collisionGroupDoor)) --> falseCollisionGroupId
Color
Contoh Kode
Warna Badan Kesehatan Karakter
-- Tempelkan ke dalam Skrip di dalam StarterCharacterScripts
-- Lalu mainkan permainan, dan sesuaikan kesehatan karakter Anda
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
-- Buat warna dengan tweening berdasarkan persentase kesehatan Anda
-- Warna bergerak dari colorHealthy (100%) ----- > colorUnhealthy (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)CustomPhysicalProperties
Contoh Kode
Tetapkan Properti Fisik Kustom
local part = script.Parent
-- Ini akan membuat bagian menjadi ringan dan memantul!
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 = physPropertiesElasticity
Friction
FrontParamA
FrontParamB
FrontSurfaceInput
LeftParamA
LeftParamB
LeftSurfaceInput
Locked
Contoh Kode
Unlock Rekursif
-- Tempelkan ke dalam Script di dalam Model yang ingin Anda buka kunci
local model = script.Parent
-- Fungsi ini melakukan rekursi melalui hierarki model dan membuka
-- setiap bagian yang ditemui.
local function recursiveUnlock(object)
if object:IsA("BasePart") then
object.Locked = false
end
-- Panggil fungsi yang sama pada anak-anak objek
-- Proses rekursif berhenti jika sebuah objek tidak memiliki anak
for _, child in pairs(object:GetChildren()) do
recursiveUnlock(child)
end
end
recursiveUnlock(model)Orientation
Contoh Kode
Penggulung Bagian
local part = script.Parent
local INCREMENT = 360 / 20
-- Putar bagian secara terus-menerus
while true do
for degrees = 0, 360, INCREMENT do
-- Atur hanya rotasi sumbu Y
part.Rotation = Vector3.new(0, degrees, 0)
-- Cara yang lebih baik untuk melakukan ini adalah dengan mengatur CFrame
--part.CFrame = CFrame.new(part.Position) * CFrame.Angles(0, math.rad(degrees), 0)
task.wait()
end
endPivotOffset
Contoh Kode
Atur Ulang Pivot
local function resetPivot(model)
local boundsCFrame = model:GetBoundingBox()
if model.PrimaryPart then
-- Menetapkan PivotOffset untuk bagian utama model
model.PrimaryPart.PivotOffset = model.PrimaryPart.CFrame:ToObjectSpace(boundsCFrame)
else
-- Menetapkan WorldPivot untuk model
model.WorldPivot = boundsCFrame
end
end
resetPivot(script.Parent)Jarum Jam
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
-- Buat dial
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
-- Buat jarum
local hourHand = createHand(7, 1, 0)
local minuteHand = createHand(10, 0.6, 0.1)
local secondHand = createHand(11, 0.2, 0.2)
-- Jalankan jam
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()
endResizeableFaces
Contoh Kode
Pegangan Ulang Ukur
-- Tempatkan Skrip ini di beberapa jenis BasePart, seperti
-- Part, TrussPart, WedgePart, CornerWedgePart, dll.
local part = script.Parent
-- Buat objek pegangan untuk bagian ini
local handles = Instance.new("Handles")
handles.Adornee = part
handles.Parent = part
-- Secara manual tentukan wajah yang berlaku untuk pegangan ini
handles.Faces = Faces.new(Enum.NormalId.Top, Enum.NormalId.Front, Enum.NormalId.Left)
-- Sebagai alternatif, gunakan wajah di mana bagian dapat diubah ukurannya.
-- Jika bagian adalah TrussPart dengan hanya dua dimensi Ukuran
-- panjang 2, maka ResizeableFaces hanya akan memiliki dua
-- wajah yang diaktifkan. Untuk bagian lainnya, semua wajah akan diaktifkan.
handles.Faces = part.ResizeableFacesRightParamA
RightParamB
RightSurfaceInput
RotVelocity
Size
Contoh Kode
Pembuat Piramida
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 dari color0 dan 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 = workspaceSpecificGravity
TopParamA
TopParamB
TopSurfaceInput
Transparency
Contoh Kode
Pintu Memudar
-- Tempelkan di dalam Skrip di dalam bagian yang tinggi
local part = script.Parent
local OPEN_TIME = 1
-- Apakah pintu dapat dibuka saat ini?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Hitam")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Biru terang")
end
local function onTouch(otherPart)
-- Jika pintu sudah terbuka, tidak ada tindakan yang diambil
if debounce then
print("D")
return
end
-- Periksa jika disentuh oleh Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("bukan manusia")
return
end
-- Lakukan urutan pembukaan pintu
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()Velocity
Metode
AngularAccelerationToTorque
ApplyAngularImpulse
ApplyImpulseAtPosition
BreakJoints
breakJoints
CanCollideWith
CanSetNetworkOwnership
Memberikan nilai
Contoh Kode
Periksa Apakah Kepemilikan Jaringan Suatu Bagian Dapat Diatur
local part = workspace:FindFirstChild("Part")
if part and part:IsA("BasePart") then
local canSet, errorReason = part:CanSetNetworkOwnership()
if canSet then
print(part:GetFullName() .. " kepemilikan jaringannya dapat diubah!")
else
warn("Tidak dapat mengubah kepemilikan jaringan dari " .. part:GetFullName() .. " karena: " .. errorReason)
end
endGetClosestPointOnSurface
GetConnectedParts
GetJoints
BasePart:GetJoints():Instances
Memberikan nilai
Instances
GetMass
getMass
GetNoCollisionConstraints
BasePart:GetNoCollisionConstraints():Instances
Memberikan nilai
Instances
GetRenderCFrame
GetRootPart
GetTouchingParts
BasePart:GetTouchingParts():Instances
Memberikan nilai
Instances
GetVelocityAtPosition
IntersectAsync
BasePart:IntersectAsync(
Parameter
parts:Instances |
| Nilai Default: "Default" |
| Nilai Default: "Automatic" |
Memberikan nilai
MakeJoints
makeJoints
Resize
Parameter
Memberikan nilai
resize
SetNetworkOwner
SetNetworkOwnershipAuto
BasePart:SetNetworkOwnershipAuto():()
Memberikan nilai
()
SubtractAsync
BasePart:SubtractAsync(
Parameter
parts:Instances |
| Nilai Default: "Default" |
| Nilai Default: "Automatic" |
Memberikan nilai
Contoh Kode
BasePart:SubtractAsync()
local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Lakukan operasi pengurangan
local success, newSubtract = pcall(function()
return mainPart:SubtractAsync(otherParts)
end)
-- Jika operasi berhasil, posisikannya pada lokasi yang sama dan jadikan sebagai anak dari workspace
if success and newSubtract then
newSubtract.Position = mainPart.Position
newSubtract.Parent = Workspace
end
-- Hancurkan bagian asli yang tetap utuh setelah operasi
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
endTorqueToAngularAcceleration
UnionAsync
BasePart:UnionAsync(
Parameter
parts:Instances |
| Nilai Default: "Default" |
| Nilai Default: "Automatic" |
Memberikan nilai
Contoh Kode
BasePart:UnionAsync()
local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Lakukan operasi gabungan
local success, newUnion = pcall(function()
return mainPart:UnionAsync(otherParts)
end)
-- Jika operasi berhasil, posisikan di lokasi yang sama dan jadikan sebagai anak workspace
if success and newUnion then
newUnion.Position = mainPart.Position
newUnion.Parent = Workspace
end
-- Hancurkan bagian asli yang tetap utuh setelah operasi
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
endAcara
LocalSimulationTouched
OutfitChanged
StoppedTouching
Touched
Parameter
Contoh Kode
Jumlah Bagian yang Disentuh
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("Sentuhan dimulai: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Sentuhan berakhir: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)Model Tersentuh
local model = script.Parent
local function onTouched(otherPart)
-- Abaikan instance dari model yang bersentuhan dengan dirinya sendiri
if otherPart:IsDescendantOf(model) then
return
end
print(model.Name .. " bertabrakan dengan " .. otherPart.Name)
end
for _, child in pairs(model:GetChildren()) do
if child:IsA("BasePart") then
child.Touched:Connect(onTouched)
end
endTouchEnded
Parameter
Contoh Kode
Jumlah Bagian yang Disentuh
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("Sentuhan dimulai: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Sentuhan berakhir: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)