Lớp công cụ
Model
*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
AddPersistentPlayer(playerInstance: Player):() |
BreakJoints():() |
breakJoints():() |
MakeJoints():() |
makeJoints():() |
RemovePersistentPlayer(playerInstance: Player):() |
SetPrimaryPartCFrame(cframe: CFrame):() |
TranslateBy(delta: Vector3):() |
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ã
Khởi Tạo Mô Hình Cơ Bản
local function groupObjects(objectTable)
local model = Instance.new("Model")
for _, object in pairs(objectTable) do
object.Parent = model
end
return model
end
local objects = {
Instance.new("Part"),
Instance.new("Part"),
}
groupObjects(objects)Tài Liệu Tham Khảo API
Thuộc Tính
PrimaryPart
Mẫu mã
Ném Xúc Xắc
-- Tạo một mô hình xúc xắc với hai nửa và gắn chúng lại với nhau
local diceModel = Instance.new("Model")
diceModel.Name = "ChanceCube"
local diceTop = Instance.new("Part")
diceTop.Size = Vector3.new(4, 2, 4)
diceTop.Position = Vector3.new(0, 1, 0)
diceTop.Color = Color3.new(0, 0, 1)
diceTop.Parent = diceModel
local diceBottom = diceTop:Clone()
diceBottom.Position = Vector3.new(0, -1, 0)
diceBottom.Color = Color3.new(1, 0, 0)
diceBottom.Parent = diceModel
local weld = Instance.new("WeldConstraint")
weld.Part0 = diceTop
weld.Part1 = diceBottom
weld.Parent = diceModel
-- Đưa xúc xắc lên không trung trên gốc làm việc (không cần phần chính)
diceModel.Parent = workspace
diceModel:PivotTo(CFrame.new(0, 10, 0))
-- Gán phần chính trước khi mô phỏng vật lý
-- Nếu không có dòng này, kịch bản sẽ luôn xuất ra cùng một thứ và hộp bao quanh của mô hình sẽ không thay đổi hướng
diceModel.PrimaryPart = diceTop
-- Chờ một chút trước khi ném xúc xắc (để nó ổn định trên sàn)
for i = 5, 1, -1 do
print("Đang ném xúc xắc trong...", i)
task.wait(1)
end
diceTop:ApplyAngularImpulse(Vector3.new(15000, 1000, 5000))
diceTop:ApplyImpulse(Vector3.new(0, 3000, 0))
task.wait(1)
-- Chờ cho quá trình ném hoàn tất
while diceTop.AssemblyLinearVelocity.Magnitude > 0.1 or diceTop.AssemblyAngularVelocity.Magnitude > 0.1 do
task.wait()
end
-- Lấy hướng của xúc xắc, bị ảnh hưởng bởi phần chính
local orientation = diceModel:GetBoundingBox()
if orientation.YVector.Y > 0.5 then
print("Đó là cậu bé!")
else
print("Đó là mẹ của cậu ấy!")
endWorldPivot
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)Trục Thế Giới
local Workspace = game:GetService("Workspace")
local bluePart = Instance.new("Part")
bluePart.Name = "BluePart"
bluePart.Parent = Workspace
local redPart = Instance.new("Part")
redPart.Name = "RedPart"
redPart.Parent = Workspace
local model = Instance.new("Model")
Workspace.BluePart.Parent = model
Workspace.RedPart.Parent = model
model.Parent = Workspace
print(model:GetPivot()) -- Hiện tại bằng với trung tâm của hộp chứa "BluePart" và "RedPart"
model:PivotTo(CFrame.new(0, 10, 0)) -- Điều này hoạt động mà không cần phải thiết lập rõ ràng "model.WorldPivot"Phương Pháp
AddPersistentPlayer
BreakJoints
breakJoints
GetBoundingBox
Lợi Nhuận
Mẫu mã
Lấy Hộp Bao
local Workspace = game:GetService("Workspace")
local model = Workspace.Model
local part = Workspace.Part
local orientation, size = model:GetBoundingBox()
-- Thay đổi kích thước và vị trí của phần bằng với hộp bao của mô hình
part.Size = size
part.CFrame = orientationGetExtentsSize
Lợi Nhuận
Mẫu mã
Kích thước GetExtentsSize của Mô hình
local model = Instance.new("Model")
model.Parent = workspace
local RNG = Random.new()
for _ = 1, 5 do
local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(RNG:NextNumber(0.05, 5), RNG:NextNumber(0.05, 5), RNG:NextNumber(0.05, 5))
part.Parent = model
end
print(model:GetExtentsSize())GetModelCFrame
GetModelSize
GetPrimaryPartCFrame
GetScale
Lợi Nhuận
Mẫu mã
Thay thế một mô hình thay thế bằng cách sử dụng PivotTo và ScaleTo
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Tìm tất cả các mô hình với thẻ mà chúng ta muốn thay thế
local items = CollectionService:GetTagged("Tree")
local newModel = ReplicatedStorage.FancyTreeReplacementModel
for _, item in items do
-- Tạo mục mới và thay đổi kích thước / vị trí nó ở nơi mà mục cũ đã có
local newItem = newModel:Clone()
newItem:ScaleTo(item:GetScale())
newItem:PivotTo(item:GetPivot())
-- Thêm cùng một thẻ vào mô hình thay thế
CollectionService:AddTag(newItem, "Tree")
-- Xóa mục cũ và gán cha cho mục mới
newItem.Parent = item.Parent
item:Destroy()
endMakeJoints
makeJoints
move
MoveTo
Tham Số
Lợi Nhuận
()
Mẫu mã
Di chuyển mô hình
local START_POSITION = Vector3.new(-20, 10, 0)
local END_POSITION = Vector3.new(0, 10, 0)
local model = Instance.new("Model")
model.Parent = workspace
local part1 = Instance.new("Part")
part1.Size = Vector3.new(4, 4, 4)
part1.Position = START_POSITION
part1.Anchored = true
part1.BrickColor = BrickColor.new("Màu vàng sáng")
part1.Parent = model
local part2 = Instance.new("Part")
part2.Size = Vector3.new(2, 2, 2)
part2.Position = START_POSITION + Vector3.new(0, 3, 0)
part2.Anchored = true
part2.BrickColor = BrickColor.new("Màu xanh sáng")
part2.Parent = model
model.PrimaryPart = part1
model.Parent = workspace
local obstruction = Instance.new("Part")
obstruction.Name = "Cản"
obstruction.Size = Vector3.new(10, 10, 10)
obstruction.Position = Vector3.new(0, 10, 0)
obstruction.Anchored = true
obstruction.BrickColor = BrickColor.new("Màu xanh lá sáng")
obstruction.Parent = workspace
task.wait(3)
model:MoveTo(END_POSITION)moveTo
RemovePersistentPlayer
ResetOrientationToIdentity
SetIdentityOrientation
SetPrimaryPartCFrame
TranslateBy
Tham Số
Lợi Nhuận
()
Mẫu mã
Mô hình Dịch Bởi
local START_POSITION = Vector3.new(-20, 10, 0)
local END_POSITION = Vector3.new(0, 10, 0)
local model = Instance.new("Model")
local part1 = Instance.new("Part")
part1.Size = Vector3.new(4, 4, 4)
part1.CFrame = CFrame.new(START_POSITION) * CFrame.Angles(0, math.rad(45), 0)
part1.Anchored = true
part1.BrickColor = BrickColor.new("Vàng sáng")
part1.Parent = model
local part2 = Instance.new("Part")
part2.Size = Vector3.new(2, 2, 2)
part2.CFrame = part1.CFrame * CFrame.new(0, 3, 0)
part2.Anchored = true
part2.BrickColor = BrickColor.new("Xanh dương sáng")
part2.Parent = model
model.PrimaryPart = part1
model.Parent = workspace
local obstruction = Instance.new("Part")
obstruction.Name = "Cản"
obstruction.Size = Vector3.new(10, 10, 10)
obstruction.Position = Vector3.new(0, 10, 0)
obstruction.Transparency = 0.5
obstruction.Anchored = true
obstruction.BrickColor = BrickColor.new("Xanh lá sáng")
obstruction.Parent = workspace
task.wait(3)
-- sử dụng TranslateBy để dịch chuyển mô hình vào trong phần cản
model:TranslateBy(END_POSITION - START_POSITION)