BasePart

Visualizza obsoleti

*Questo contenuto è tradotto usando AI (Beta) e potrebbe contenere errori. Per visualizzare questa pagina in inglese, clicca qui.

Non costruibile
Non Navigabile

BasePart è una classe base astratta per gli oggetti nel mondo che rendono e sono fisicamente simulati mentre sono nel Workspace .Ci sono diverse implementazioni di BasePart , la più comune è Part e MeshPart .Altri includono WedgePart , SpawnLocation , e l'oggetto singleton Terrain.Generalmente, quando la documentazione si riferisce a una "parte", la maggior parte delle implementazioni BasePart lavorerà e non solo Part .

Per informazioni su come BaseParts sono gruppati in corpi rigidi simulati, vedi Assemblaggi .

Ci sono molti oggetti diversi che interagiscono con BasePart (diversi da Terrain ), tra cui:

Sommario

Proprietà

Proprietà provenienti da PVInstance

Proprietà

Metodi

Metodi provenienti da PVInstance

Metodi

Eventi

Proprietà

Anchored

Lettura Parallela

Campioni di codice

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

Non Replicato
Lettura Parallela

AssemblyCenterOfMass

Sola Lettura
Non Replicato
Lettura Parallela

AssemblyLinearVelocity

Non Replicato
Lettura Parallela

AssemblyMass

Sola Lettura
Non Replicato
Lettura Parallela

AssemblyRootPart

Sola Lettura
Non Replicato
Lettura Parallela

AudioCanCollide

Lettura Parallela

BackSurface

Lettura Parallela

BottomSurface

Lettura Parallela

BrickColor

Non Replicato
Lettura Parallela

CFrame

Lettura Parallela

Campioni di codice

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

Lettura Parallela

Campioni di codice

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

Lettura Parallela

CanTouch

Lettura Parallela

CastShadow

Lettura Parallela

CenterOfMass

Sola Lettura
Non Replicato
Lettura Parallela

CollisionGroup

Non Replicato
Lettura Parallela

Campioni di codice

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

Non Replicato
Lettura Parallela

Campioni di codice

Colore del corpo della salute del personaggio

-- Incolla in uno script all'interno di StarterCharacterScripts
-- Quindi gioca al gioco e gioca con la salute del tuo personaggio
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
-- Crea un colore in base alla percentuale della tua salute tweening
-- Il colore va da 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)

CurrentPhysicalProperties

Sola Lettura
Non Replicato
Lettura Parallela

CustomPhysicalProperties

Lettura Parallela

Campioni di codice

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

Lettura Parallela

ExtentsCFrame

Sola Lettura
Non Replicato
Lettura Parallela

ExtentsSize

Sola Lettura
Non Replicato
Lettura Parallela

FrontSurface

Lettura Parallela

LeftSurface

Lettura Parallela

LocalTransparencyModifier

Nascosto
Non Replicato
Lettura Parallela

Locked

Lettura Parallela

Campioni di codice

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

Sola Lettura
Non Replicato
Lettura Parallela

Massless

Lettura Parallela
Lettura Parallela

MaterialVariant

Non Replicato
Lettura Parallela

Orientation

Nascosto
Non Replicato
Lettura Parallela

Campioni di codice

Spinner di parte

local part = script.Parent
local INCREMENT = 360 / 20
-- Ruota la parte continuamente
while true do
for degrees = 0, 360, INCREMENT do
-- Imposta solo la rotazione dell'asse Y
part.Rotation = Vector3.new(0, degrees, 0)
-- Un modo migliore per farlo sarebbe impostare CFrame
--part.CFrame = CFrame.new(part.Position) * CFrame.Angoli(0, matematica.rad(gradi), 0)
task.wait()
end
end

PivotOffset

Lettura Parallela

Campioni di codice

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)
Orologio a mano

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
-- Crea chiamata
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
-- Crea le mani
local hourHand = createHand(7, 1, 0)
local minuteHand = createHand(10, 0.6, 0.1)
local secondHand = createHand(11, 0.2, 0.2)
-- Esegui orologio
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

Nascosto
Non Replicato
Lettura Parallela

ReceiveAge

Nascosto
Sola Lettura
Non Replicato
Lettura Parallela

Reflectance

Lettura Parallela

ResizeIncrement

Sola Lettura
Non Replicato
Lettura Parallela

ResizeableFaces

Sola Lettura
Non Replicato
Lettura Parallela

Campioni di codice

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

Lettura Parallela

RootPriority

Lettura Parallela

Rotation

Non Replicato
Lettura Parallela
Non Replicato
Lettura Parallela

Campioni di codice

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
Lettura Parallela

Transparency

Lettura Parallela

Campioni di codice

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()

Metodi

AngularAccelerationToTorque

Parametri

angAcceleration: Vector3
Valore predefinito: ""
angVelocity: Vector3
Valore predefinito: "0, 0, 0"

Restituzioni

ApplyAngularImpulse

()

Parametri

impulse: Vector3
Valore predefinito: ""

Restituzioni

()

ApplyImpulse

()

Parametri

impulse: Vector3
Valore predefinito: ""

Restituzioni

()

ApplyImpulseAtPosition

()

Parametri

impulse: Vector3
Valore predefinito: ""
position: Vector3
Valore predefinito: ""

Restituzioni

()

CanCollideWith

Scrivi Parallelo

Parametri

part: BasePart
Valore predefinito: ""

Restituzioni

CanSetNetworkOwnership


Restituzioni

Campioni di codice

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

Parametri

position: Vector3
Valore predefinito: ""

Restituzioni

GetConnectedParts

Instances
Scrivi Parallelo

Parametri

recursive: boolean
Valore predefinito: false

Restituzioni

Instances

GetJoints

Instances
Scrivi Parallelo

Restituzioni

Instances

GetMass

Scrivi Parallelo

Restituzioni

Campioni di codice

Trovare la massa di una parte

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

Scrivi Parallelo

Restituzioni

GetNetworkOwnershipAuto

Scrivi Parallelo

Restituzioni

GetNoCollisionConstraints

Instances

Restituzioni

Instances

GetRootPart

Scrivi Parallelo

Restituzioni

GetTouchingParts

Instances

Restituzioni

Instances

GetVelocityAtPosition

Scrivi Parallelo

Parametri

position: Vector3
Valore predefinito: ""

Restituzioni

IsGrounded

Scrivi Parallelo

Restituzioni

Resize

Parametri

normalId: Enum.NormalId
Valore predefinito: ""
deltaAmount: number
Valore predefinito: ""

Restituzioni

SetNetworkOwner

()

Parametri

playerInstance: Player
Valore predefinito: "nil"

Restituzioni

()

SetNetworkOwnershipAuto

()

Restituzioni

()

TorqueToAngularAcceleration

Parametri

torque: Vector3
Valore predefinito: ""
angVelocity: Vector3
Valore predefinito: "0, 0, 0"

Restituzioni

IntersectAsync

Resa

Parametri

parts: Instances
Valore predefinito: ""
collisionfidelity: Enum.CollisionFidelity
Valore predefinito: "Default"
renderFidelity: Enum.RenderFidelity
Valore predefinito: "Automatic"

Restituzioni

SubtractAsync

Resa

Parametri

parts: Instances
Valore predefinito: ""
collisionfidelity: Enum.CollisionFidelity
Valore predefinito: "Default"
renderFidelity: Enum.RenderFidelity
Valore predefinito: "Automatic"

Restituzioni

Campioni di codice

BasePart:SubtractAsync()

local Workspace = game:GetService("Workspace")
local mainPart = script.Parent.PartA
local otherParts = { script.Parent.PartB, script.Parent.PartC }
-- Eseguire l'operazione di sottrazione
local success, newSubtract = pcall(function()
return mainPart:SubtractAsync(otherParts)
end)
-- Se l'operazione ha successo, posizionala nello stesso luogo e affidala allo spazio di lavoro
if success and newSubtract then
newSubtract.Position = mainPart.Position
newSubtract.Parent = Workspace
end
-- Distruggi le parti originali che rimangono intatte dopo l'operazione
mainPart:Destroy()
for _, part in otherParts do
part:Destroy()
end

UnionAsync

Resa

Parametri

parts: Instances
Valore predefinito: ""
collisionfidelity: Enum.CollisionFidelity
Valore predefinito: "Default"
renderFidelity: Enum.RenderFidelity
Valore predefinito: "Automatic"

Restituzioni

Campioni di codice

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

Eventi

TouchEnded

Parametri

otherPart: BasePart

Campioni di codice

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

Parametri

otherPart: BasePart

Campioni di codice

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)
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