學習
引擎類別
Model

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡


概要
方法
AddPersistentPlayer(playerInstance: Player):()
BreakJoints():()
已棄用
breakJoints():()
已棄用
GetModelSize():Vector3
已棄用
MakeJoints():()
已棄用
makeJoints():()
已棄用
move(location: Vector3):()
已棄用
MoveTo(position: Vector3):()
moveTo(location: Vector3):()
已棄用
RemovePersistentPlayer(playerInstance: Player):()
ScaleTo(newScaleFactor: number):()
SetIdentityOrientation():()
已棄用
SetPrimaryPartCFrame(cframe: CFrame):()
已棄用
TranslateBy(delta: Vector3):()
繼承成員
範例程式碼
基本模型實例化
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)

API 參考
屬性
LevelOfDetail
外掛程式安全性
平行讀取
Model.LevelOfDetail:Enum.ModelLevelOfDetail

ModelStreamingMode
平行讀取
Model.ModelStreamingMode:Enum.ModelStreamingMode

PrimaryPart
平行讀取
Model.PrimaryPart:BasePart
範例程式碼
擲骰子
-- 創建一個有兩半的骰子模型並將它們連接在一起
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
-- 將骰子放在工作空間原點上方的空中(不需要主部件)
diceModel.Parent = workspace
diceModel:PivotTo(CFrame.new(0, 10, 0))
-- 在進行物理模擬之前分配主部件
-- 若無此行,腳本將總是輸出相同的東西,模型的邊界框將無法改變方向
diceModel.PrimaryPart = diceTop
-- 在擲骰子之前等一會(讓它穩定在地板上)
for i = 5, 1, -1 do
print("擲骰子中...", i)
task.wait(1)
end
diceTop:ApplyAngularImpulse(Vector3.new(15000, 1000, 5000))
diceTop:ApplyImpulse(Vector3.new(0, 3000, 0))
task.wait(1)
-- 等待擲骰子完成
while diceTop.AssemblyLinearVelocity.Magnitude > 0.1 or diceTop.AssemblyAngularVelocity.Magnitude > 0.1 do
task.wait()
end
-- 獲取骰子的方向,由主部件影響
local orientation = diceModel:GetBoundingBox()
if orientation.YVector.Y > 0.5 then
print("是男孩!")
else
print("是他的母親!")
end

Scale
未複製
無法建立指令碼
平行讀取
Model.Scale:number

WorldPivot
未複製
平行讀取
Model.WorldPivot:CFrame
範例程式碼
重置樞紐
-- 定義函數以重置樞紐
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)

方法
AddPersistentPlayer
Model:AddPersistentPlayer(playerInstance:Player):()
參數
playerInstance:Player
預設值:"nil"
返回
()

BreakJoints
已棄用

breakJoints
已棄用

GetBoundingBox
Model:GetBoundingBox():Tuple
返回

GetExtentsSize
Model:GetExtentsSize():Vector3
返回
範例程式碼
模型獲取範圍大小
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
已棄用

GetPersistentPlayers
Model:GetPersistentPlayers():{Player}
返回

GetPrimaryPartCFrame
已棄用

GetScale
模擬存取權
Model:GetScale():number
返回
範例程式碼
使用 PivotTo 和 ScaleTo 來替換替換模型
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- 找到所有帶有我們要替換的標籤的模型
local items = CollectionService:GetTagged("Tree")
local newModel = ReplicatedStorage.FancyTreeReplacementModel
for _, item in items do
-- 製作新物品並在舊物品的位置進行縮放 / 定位
local newItem = newModel:Clone()
newItem:ScaleTo(item:GetScale())
newItem:PivotTo(item:GetPivot())
-- 將相同的標籤添加到替換物品
CollectionService:AddTag(newItem, "Tree")
-- 刪除舊物品並將新物品置於父物件中
newItem.Parent = item.Parent
item:Destroy()
end

MakeJoints
已棄用

makeJoints
已棄用

move
已棄用

MoveTo
Model:MoveTo(position:Vector3):()
參數
position:Vector3
返回
()
範例程式碼
模型移動到
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("Bright yellow")
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("Bright blue")
part2.Parent = model
model.PrimaryPart = part1
model.Parent = workspace
local obstruction = Instance.new("Part")
obstruction.Name = "障礙物"
obstruction.Size = Vector3.new(10, 10, 10)
obstruction.Position = Vector3.new(0, 10, 0)
obstruction.Anchored = true
obstruction.BrickColor = BrickColor.new("Bright green")
obstruction.Parent = workspace
task.wait(3)
model:MoveTo(END_POSITION)

moveTo
已棄用

RemovePersistentPlayer
Model:RemovePersistentPlayer(playerInstance:Player):()
參數
playerInstance:Player
預設值:"nil"
返回
()

ResetOrientationToIdentity
已棄用

ScaleTo
Model:ScaleTo(newScaleFactor:number):()
參數
newScaleFactor:number
返回
()

SetIdentityOrientation
已棄用

SetPrimaryPartCFrame
已棄用

TranslateBy
Model:TranslateBy(delta:Vector3):()
參數
delta:Vector3
返回
()
範例程式碼
模型 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)
-- 使用 TranslateBy 將模型移入障礙物
model:TranslateBy(END_POSITION - START_POSITION)

©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。