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 bên trên gốc không gian làm việc (không yêu cầu một 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, script sẽ luôn xuất cùng một thứ và bounding box của mô hình sẽ không thay đổi hướng
diceModel.PrimaryPart = diceTop
-- Chờ một chút trước khi lăn xúc xắc (để nó ổn định xuống sàn)
for i = 5, 1, -1 do
print("Đang lăn 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 việc lăn 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 nó!")
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)Phương Pháp
AddPersistentPlayer
BreakJoints
breakJoints
GetExtentsSize
Lợi Nhuận
Mẫu mã
Lấy kích thước Bounding Box 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à thu nhỏ / định vị nó ở nơi mục cũ
local newItem = newModel:Clone()
newItem:ScaleTo(item:GetScale())
newItem:PivotTo(item:GetPivot())
-- Thêm cùng một thẻ vào mục thay thế
CollectionService:AddTag(newItem, "Tree")
-- Xóa mục cũ và bố trí mục mới
newItem.Parent = item.Parent
item:Destroy()
endMakeJoints
makeJoints
move
MoveTo
Tham Số
Lợi Nhuận
()
Mẫu mã
Mô Hình Di Chuyển Đến
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 TranslateBy
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("Bright yellow")
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("Bright blue")
part2.Parent = model
model.PrimaryPart = part1
model.Parent = workspace
local obstruction = Instance.new("Part")
obstruction.Name = "Obstruction"
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("Bright green")
obstruction.Parent = workspace
task.wait(3)
-- sử dụng TranslateBy để chuyển mô hình vào phần cản
model:TranslateBy(END_POSITION - START_POSITION)