【发布时间】:2020-06-29 04:54:56
【问题描述】:
我正在尝试制作构建脚本,但遇到了问题。我的函数 move() 在将克隆模型作为父级放入工作区时会弹出一个错误,因为它无法设置 CFrame pos .. 有什么方法可以阻止这种情况发生吗?错误:Model:SetPrimaryCFrame() 失败,因为没有设置 PrimaryPart,或者 PrimaryPart 不再存在。请在使用前设置 Model.PrimaryPart。
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local model = game.ReplicatedStorage.WoodCrate:Clone()
local gridSize = 2
local cratebutton = script.Parent:WaitForChild("brick").TextButton
local allowed = false
local x
local y
local z
local canPlace = false
local isPlace = false
local canStart = true
local function grid()
x = math.floor(mouse.Hit.X / gridSize + 0.5) * gridSize
y = 2
z = math.floor(mouse.Hit.Z / gridSize + 0.5) * gridSize
end
function move()
mouse.TargetFilter = model
grid()
model:SetPrimaryPartCFrame(CFrame.new(x,y,z))
end
function placingObject()
if canPlace and isPlace then
local modelClone = model:Clone()
modelClone.Parent = workspace.objFolder
canStart = true
isPlace = false
canPlace = false
model:Destroy()
end
end
function ChoosingPlacement()
if canStart then
model.Parent = workspace
mouse.Move:Connect(move)
canStart = false
canPlace = true
isPlace = true
end
end
cratebutton.MouseButton1Click:Connect(ChoosingPlacement)
mouse.Button1Down:Connect(placingObject)
【问题讨论】:
-
如果您检查 ReplicatedStorage 中的 WoodCrate,是否将任何内容设置为 PrimaryPart?
-
是的,确实如此。只是我认为我将 model:Destroy() 放在导致错误的位置,但不确定在这种情况下如何避免它。