Lớp công cụ
BasePart
*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.
Tóm Tắt
Thuộc Tính
Phương Pháp
Sự Kiện
StoppedTouching(otherPart: BasePart):RBXScriptSignal |
Touched(otherPart: BasePart):RBXScriptSignal |
TouchEnded(otherPart: BasePart):RBXScriptSignal |
Thành viên kế thừa
4 thành viên kế thừa từ PVInstance
57 thành viên kế thừa từ Instance
6 thành viên kế thừa từ Object
Được kế thừa bởi
Mẫu mã
Làm mượt CFrame của một Part
local TweenService = game:GetService("TweenService")
-- tạo một phần và mục tiêu
local part = Instance.new("Part", workspace)
part.Size = Vector3.one
part.Anchored = true
part.BrickColor = BrickColor.new("Electric blue")
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("Really red")
target.CFrame = CFrame.new(0, 8, 0)
-- thiết lập một biến để lưu trữ tốc độ
local velocity = CFrame.new()
-- điều chỉnh tốc độ mượt mà
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)Tài Liệu Tham Khảo API
Thuộc Tính
Anchored
Mẫu mã
Chuyển Đổi Cố Định Bộ Phận
local part = script.Parent
-- Tạo một ClickDetector để chúng ta có thể biết khi nào bộ phận được nhấp
local cd = Instance.new("ClickDetector", part)
-- Hàm này cập nhật cách mà bộ phận trông dựa trên trạng thái Đã Cố Định của nó
local function updateVisuals()
if part.Anchored then
-- Khi bộ phận đã cố định...
part.BrickColor = BrickColor.new("Đỏ sáng")
part.Material = Enum.Material.DiamondPlate
else
-- Khi bộ phận không còn cố định...
part.BrickColor = BrickColor.new("Vàng sáng")
part.Material = Enum.Material.Wood
end
end
local function onToggle()
-- Chuyển đổi thuộc tính đã cố định
part.Anchored = not part.Anchored
-- Cập nhật trạng thái hình ảnh của khối
updateVisuals()
end
-- Cập nhật, sau đó bắt đầu lắng nghe các cú nhấp chuột
updateVisuals()
cd.MouseClick:Connect(onToggle)BackParamA
BackParamB
BackSurfaceInput
BottomParamA
BottomParamB
BottomSurfaceInput
brickColor
CanCollide
Mẫu mã
Cửa Mờ
-- Dán vào một Script bên trong một phần cao
local part = script.Parent
local OPEN_TIME = 1
-- Cửa có thể được mở vào lúc này không?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Đen")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Xanh sáng")
end
local function onTouch(otherPart)
-- Nếu cửa đã mở rồi, thì không làm gì cả
if debounce then
print("D")
return
end
-- Kiểm tra nếu bị chạm bởi một Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("không phải con người")
return
end
-- Thực hiện chuỗi mở cửa
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()CFrame
Mẫu mã
Cài đặt CFrame của Phần
local part = script.Parent:WaitForChild("Part")
local otherPart = script.Parent:WaitForChild("OtherPart")
-- Đặt lại CFrame của phần về (0, 0, 0) mà không có xoay.
-- Điều này đôi khi được gọi là CFrame "danh tính"
part.CFrame = CFrame.new()
-- Đặt vào một vị trí cụ thể (X, Y, Z)
part.CFrame = CFrame.new(0, 25, 10)
-- Giống như trên, nhưng sử dụng một Vector3
local point = Vector3.new(0, 25, 10)
part.CFrame = CFrame.new(point)
-- Đặt CFrame của phần ở một điểm, nhìn vào một điểm khác
local lookAtPoint = Vector3.new(0, 20, 15)
part.CFrame = CFrame.lookAt(point, lookAtPoint)
-- Xoay CFrame của phần theo pi/2 radians trên trục X địa phương
part.CFrame = part.CFrame * CFrame.Angles(math.pi / 2, 0, 0)
-- Xoay CFrame của phần theo 45 độ trên trục Y địa phương
part.CFrame = part.CFrame * CFrame.Angles(0, math.rad(45), 0)
-- Xoay CFrame của phần theo 180 độ trên trục Z toàn cầu (chú ý thứ tự!)
part.CFrame = CFrame.Angles(0, 0, math.pi) * part.CFrame -- Pi radians bằng 180 độ
-- Kết hợp hai CFrames được thực hiện bằng cách sử dụng * (toán tử nhân)
part.CFrame = CFrame.new(2, 3, 4) * CFrame.new(4, 5, 6) --> tương đương với CFrame.new(6, 8, 10)
-- Không giống như nhân đại số, kết hợp CFrame KHÔNG phải giao hoán: a * b không nhất thiết là b * a!
-- Hãy tưởng tượng * như một chuỗi hành động CÓ THỨ TỰ. Ví dụ, các dòng sau sẽ tạo ra CFrames khác nhau:
-- 1) Trượt phần 5 đơn vị trên trục X.
-- 2) Xoay phần 45 độ quanh trục Y của nó.
part.CFrame = CFrame.new(5, 0, 0) * CFrame.Angles(0, math.rad(45), 0)
-- 1) Xoay phần 45 độ quanh trục Y của nó.
-- 2) Trượt phần 5 đơn vị trên trục X.
part.CFrame = CFrame.Angles(0, math.rad(45), 0) * CFrame.new(5, 0, 0)
-- Không có "phép chia CFrame", nhưng thay vào đó đơn giản là "thực hiện phép toán nghịch đảo".
part.CFrame = CFrame.new(4, 5, 6) * CFrame.new(4, 5, 6):Inverse() --> bằng CFrame.new(0, 0, 0)
part.CFrame = CFrame.Angles(0, 0, math.pi) * CFrame.Angles(0, 0, math.pi):Inverse() --> bằng CFrame.Angles(0, 0, 0)
-- Đặt một phần tương đối với phần khác (trong trường hợp này, đặt phần của chúng tôi lên trên otherPart)
part.CFrame = otherPart.CFrame * CFrame.new(0, part.Size.Y / 2 + otherPart.Size.Y / 2, 0)CollisionGroup
Mẫu mã
PhysicsService:RegisterCollisionGroup
local PhysicsService = game:GetService("PhysicsService")
local collisionGroupBall = "CollisionGroupBall"
local collisionGroupDoor = "CollisionGroupDoor"
-- Đăng ký các nhóm va chạm
PhysicsService:RegisterCollisionGroup(collisionGroupBall)
PhysicsService:RegisterCollisionGroup(collisionGroupDoor)
-- Gán các phần cho các nhóm va chạm
script.Parent.BallPart.CollisionGroup = collisionGroupBall
script.Parent.DoorPart.CollisionGroup = collisionGroupDoor
-- Đặt các nhóm không thể va chạm với nhau và kiểm tra kết quả
PhysicsService:CollisionGroupSetCollidable(collisionGroupBall, collisionGroupDoor, false)
print(PhysicsService:CollisionGroupsAreCollidable(collisionGroupBall, collisionGroupDoor)) --> falseCollisionGroupId
Color
Mẫu mã
Màu cơ thể sức khỏe nhân vật
-- Dán vào một Script trong StarterCharacterScripts
-- Sau đó chơi trò chơi, và tùy chỉnh sức khỏe của nhân vật của bạn
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
-- Tạo một màu bằng cách tweening dựa trên tỷ lệ phần trăm sức khỏe của bạn
-- Màu sẽ chuyển từ 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
Mẫu mã
Đặt Thuộc Tính Vật Lý Tùy Chỉnh
local part = script.Parent
-- Điều này sẽ làm cho phần trở nên nhẹ và đàn hồi!
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
Mẫu mã
Mở Khóa Đệ Quy
-- Dán vào một Script trong một Mô hình bạn muốn mở khóa
local model = script.Parent
-- Hàm này đệ quy qua cấu trúc cây của một mô hình và mở khóa
-- mọi phần mà nó gặp phải.
local function recursiveUnlock(object)
if object:IsA("BasePart") then
object.Locked = false
end
-- Gọi cùng một hàm trên các đối tượng con của đối tượng
-- Quá trình đệ quy dừng lại nếu một đối tượng không có con
for _, child in pairs(object:GetChildren()) do
recursiveUnlock(child)
end
end
recursiveUnlock(model)Orientation
Mẫu mã
Bánh Xe Phần
local part = script.Parent
local INCREMENT = 360 / 20
-- Xoay phần liên tục
while true do
for degrees = 0, 360, INCREMENT do
-- Chỉ đặt xoay trên trục Y
part.Rotation = Vector3.new(0, degrees, 0)
-- Một cách tốt hơn để làm điều này là đặt CFrame
--part.CFrame = CFrame.new(part.Position) * CFrame.Angles(0, math.rad(degrees), 0)
task.wait()
end
endPivotOffset
Mẫu mã
Đặt lại 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)Kim đồng hồ
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
-- Tạo mặt đồng hồ
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
-- Tạo kim đồng hồ
local hourHand = createHand(7, 1, 0)
local minuteHand = createHand(10, 0.6, 0.1)
local secondHand = createHand(11, 0.2, 0.2)
-- Chạy đồng hồ
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
Mẫu mã
Công cụ điều chỉnh kích thước
-- Đặt Script này vào nhiều loại BasePart khác nhau, như
-- Part, TrussPart, WedgePart, CornerWedgePart, v.v.
local part = script.Parent
-- Tạo một đối tượng handles cho phần này
local handles = Instance.new("Handles")
handles.Adornee = part
handles.Parent = part
-- Chỉ định thủ công các mặt áp dụng cho công cụ điều chỉnh này
handles.Faces = Faces.new(Enum.NormalId.Top, Enum.NormalId.Front, Enum.NormalId.Left)
-- Ngược lại, sử dụng các mặt mà phần có thể được thay đổi kích thước.
-- Nếu phần là một TrussPart chỉ với hai kích thước kích thước
-- có chiều dài 2, thì ResizeableFaces sẽ chỉ có hai
-- mặt có thể sử dụng. Đối với các phần khác, tất cả các mặt sẽ được kích hoạt.
handles.Faces = part.ResizeableFacesRightParamA
RightParamB
RightSurfaceInput
RotVelocity
Size
Mẫu mã
Nhà Xây Kim Tự Tháp
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 từ color0 và 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
Mẫu mã
Cửa Mờ
-- Dán vào một Script bên trong một phần cao
local part = script.Parent
local OPEN_TIME = 1
-- Cửa có thể được mở vào lúc này không?
local debounce = false
local function open()
part.CanCollide = false
part.Transparency = 0.7
part.BrickColor = BrickColor.new("Đen")
end
local function close()
part.CanCollide = true
part.Transparency = 0
part.BrickColor = BrickColor.new("Xanh sáng")
end
local function onTouch(otherPart)
-- Nếu cửa đã mở rồi, thì không làm gì cả
if debounce then
print("D")
return
end
-- Kiểm tra nếu bị chạm bởi một Humanoid
local human = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if not human then
print("không phải con người")
return
end
-- Thực hiện chuỗi mở cửa
debounce = true
open()
task.wait(OPEN_TIME)
close()
debounce = false
end
part.Touched:Connect(onTouch)
close()Velocity
Phương Pháp
AngularAccelerationToTorque
ApplyAngularImpulse
ApplyImpulseAtPosition
BreakJoints
breakJoints
CanSetNetworkOwnership
Lợi Nhuận
Mẫu mã
Kiểm tra xem quyền sở hữu mạng của một phần có thể được thiết lập không
local part = workspace:FindFirstChild("Part")
if part and part:IsA("BasePart") then
local canSet, errorReason = part:CanSetNetworkOwnership()
if canSet then
print(part:GetFullName() .. " quyền sở hữu mạng có thể được thay đổi!")
else
warn("Không thể thay đổi quyền sở hữu mạng của " .. part:GetFullName() .. " vì: " .. errorReason)
end
endGetClosestPointOnSurface
GetConnectedParts
GetJoints
BasePart:GetJoints():Instances
Lợi Nhuận
Instances
GetMass
getMass
GetNoCollisionConstraints
BasePart:GetNoCollisionConstraints():Instances
Lợi Nhuận
Instances
GetRenderCFrame
GetRootPart
GetTouchingParts
BasePart:GetTouchingParts():Instances
Lợi Nhuận
Instances
GetVelocityAtPosition
IntersectAsync
BasePart:IntersectAsync(
Tham Số
parts:Instances |
| Giá Trị Mặc Định: "Default" |
| Giá Trị Mặc Định: "Automatic" |
Lợi Nhuận
MakeJoints
makeJoints
Resize
Tham Số
Lợi Nhuận
resize
SetNetworkOwner
SetNetworkOwnershipAuto
BasePart:SetNetworkOwnershipAuto():()
Lợi Nhuận
()
SubtractAsync
BasePart:SubtractAsync(
Tham Số
parts:Instances |
| Giá Trị Mặc Định: "Default" |
| Giá Trị Mặc Định: "Automatic" |
Lợi Nhuận
Mẫu mã
BasePart:SubtractAsync()
local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Thực hiện phép trừ
local success, newSubtract = pcall(function()
return mainPart:SubtractAsync(otherParts)
end)
-- Nếu phép toán thành công, đặt nó ở cùng vị trí và cha nó vào workspace
if success and newSubtract then
newSubtract.Position = mainPart.Position
newSubtract.Parent = Workspace
end
-- Hủy các phần gốc vẫn còn nguyên vẹn sau phép toán
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
endTorqueToAngularAcceleration
UnionAsync
BasePart:UnionAsync(
Tham Số
parts:Instances |
| Giá Trị Mặc Định: "Default" |
| Giá Trị Mặc Định: "Automatic" |
Lợi Nhuận
Mẫu mã
BasePart:UnionAsync()
local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Thực hiện phép hợp nhất
local success, newUnion = pcall(function()
return mainPart:UnionAsync(otherParts)
end)
-- Nếu phép toán thành công, đặt nó ở cùng một vị trí và gán cho workspace
if success and newUnion then
newUnion.Position = mainPart.Position
newUnion.Parent = Workspace
end
-- Huỷ các phần gốc vẫn còn nguyên vẹn sau phép toán
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
endSự Kiện
LocalSimulationTouched
OutfitChanged
StoppedTouching
Touched
Tham Số
Mẫu mã
Số lượng phần chạm
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("Bắt đầu chạm: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Kết thúc chạm: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)Mô Hình Được Chạm
local model = script.Parent
local function onTouched(otherPart)
-- Bỏ qua các trường hợp mô hình chạm vào chính nó
if otherPart:IsDescendantOf(model) then
return
end
print(model.Name .. " đã va chạm với " .. otherPart.Name)
end
for _, child in pairs(model:GetChildren()) do
if child:IsA("BasePart") then
child.Touched:Connect(onTouched)
end
endTouchEnded
Tham Số
Mẫu mã
Số lượng phần chạm
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("Bắt đầu chạm: " .. otherPart.Name)
numTouchingParts = numTouchingParts + 1
tl.Text = numTouchingParts
end
local function onTouchEnded(otherPart)
print("Kết thúc chạm: " .. otherPart.Name)
numTouchingParts = numTouchingParts - 1
tl.Text = numTouchingParts
end
part.Touched:Connect(onTouch)
part.TouchEnded:Connect(onTouchEnded)