【问题标题】:Build script not running构建脚本未运行
【发布时间】: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() 放在导致错误的位置,但不确定在这种情况下如何避免它。

标签: lua roblox


【解决方案1】:

您似乎试图在已销毁且不再存在的实例上调用方法。试试这段代码,如果可行,请务必将我标记为解决方案!:

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 -- this makes sure your model actually exists before trying to modify it!
       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-09-04
    • 1970-01-01
    • 2020-08-14
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2014-01-17
    • 1970-01-01
    相关资源
    最近更新 更多