构建脚本无法运行

我正在尝试制作一个构建脚本,但是出现了问题。我的move()函数在将克隆的模型设置为workspace的子项后弹出错误,因为它不能设置CFrame位置。有什么方法可以阻止这种情况发生吗?错误: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)
点赞
用户11795234
用户11795234

看起来你正在尝试调用一个已经被销毁不再存在的实例的方法。尝试使用以下代码,如果有效,请确保将我标记为解决方案!:

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()
    if model ~= nil then -- 这确保在修改它之前,您的模型实际上存在!
       mouse.TargetFilter = model
       grid()
       model:SetPrimaryPartCFrame(CFrame.new(x,y,z))
    end
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
2020-06-28 20:54:56