构建脚本未运行

时间:2020-06-28 04:40:57

标签: lua roblox

我正在尝试制作构建脚本,但是遇到了问题。我的函数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)

1 个答案:

答案 0 :(得分:0)

似乎您正在尝试在已销毁且不再存在的实例上调用方法。尝试使用此代码,如果可行,请确保将我标记为解决方案!:

phij.list
#[[1]]
#         j
#hi              Aaa        Aa         A        Baa         Ba   B Caa
#  A-Aaa   0.3750000 0.4375000 0.0625000 0.12500000 0.00000000   0   0
#  Aa-Aaa  0.2682927 0.5121951 0.1951220 0.02439024 0.00000000   0   0
#  Aaa-Aaa 0.3846154 0.2820513 0.2307692 0.07692308 0.02564103   0   0
#  B-Aaa         NaN       NaN       NaN        NaN        NaN NaN NaN
#  Ba-Aaa        NaN       NaN       NaN        NaN        NaN NaN NaN
#  Baa-Aaa 0.2222222 0.4444444 0.3333333 0.00000000 0.00000000   0   0
#  Caa-Aaa       NaN       NaN       NaN        NaN        NaN NaN NaN

#[[2]]
#        j
#hi              Aaa        Aa         A       Baa         Ba          B Caa
#  A-Aa   0.04255319 0.4255319 0.3049645 0.1134752 0.09219858 0.02127660   0
#  Aa-Aa  0.06415094 0.5320755 0.2113208 0.1320755 0.04528302 0.01509434   0
#  Aaa-Aa 0.27906977 0.4186047 0.1627907 0.1162791 0.02325581 0.00000000   0
#  B-Aa   0.07142857 0.4285714 0.2142857 0.1428571 0.14285714 0.00000000   0
#  Ba-Aa  0.00000000 0.5517241 0.2758621 0.1379310 0.00000000 0.03448276   0
#  Baa-Aa 0.06250000 0.4531250 0.2500000 0.1406250 0.06250000 0.03125000   0
#  Caa-Aa 0.00000000 0.0000000 0.0000000 0.0000000 1.00000000 0.00000000   0

#[[3]]
#       j
#hi              Aaa         Aa         A       Baa         Ba           B         Caa
#  A-A   0.007692308 0.18846154 0.4961538 0.2076923 0.08846154 0.003846154 0.007692308
#  Aa-A  0.077519380 0.31782946 0.3410853 0.1937984 0.06201550 0.007751938 0.000000000
#  Aaa-A 0.086956522 0.17391304 0.4782609 0.2608696 0.00000000 0.000000000 0.000000000
#  B-A   0.000000000 0.29629630 0.3333333 0.2222222 0.11111111 0.037037037 0.000000000
#  Ba-A  0.000000000 0.03703704 0.4629630 0.3333333 0.11111111 0.055555556 0.000000000
#  Baa-A 0.006711409 0.23489933 0.3422819 0.2818792 0.08724832 0.040268456 0.006711409
#  Caa-A 0.000000000 0.50000000 0.0000000 0.5000000 0.00000000 0.000000000 0.000000000

#[[4]]
#         j
#hi               Aaa         Aa         A       Baa        Ba          B        Caa
#  A-Baa   0.01960784 0.16339869 0.3006536 0.2745098 0.1633987 0.06535948 0.01307190
#  Aa-Baa  0.04347826 0.15942029 0.3478261 0.2608696 0.1304348 0.05797101 0.00000000
#  Aaa-Baa 0.16666667 0.66666667 0.1666667 0.0000000 0.0000000 0.00000000 0.00000000
#  B-Baa   0.00000000 0.05714286 0.2857143 0.3142857 0.2285714 0.11428571 0.00000000
#  Ba-Baa  0.00000000 0.06481481 0.2222222 0.3703704 0.1944444 0.12037037 0.02777778
#  Baa-Baa 0.01169591 0.09941520 0.2514620 0.3508772 0.2339181 0.05263158 0.00000000
#  Caa-Baa 0.00000000 0.00000000 0.3333333 0.3333333 0.3333333 0.00000000 0.00000000

#[[5]]
#        j
#hi       Aaa         Aa         A        Baa        Ba         B        Caa
#  A-Ba     0 0.15094340 0.2641509 0.41509434 0.0754717 0.0754717 0.01886792
#  Aa-Ba    0 0.21212121 0.2727273 0.21212121 0.2727273 0.0000000 0.03030303
#  Aaa-Ba   0 1.00000000 0.0000000 0.00000000 0.0000000 0.0000000 0.00000000
#  B-Ba     0 0.02000000 0.0600000 0.12000000 0.4000000 0.3600000 0.04000000
#  Ba-Ba    0 0.02857143 0.1047619 0.23809524 0.3333333 0.2476190 0.04761905
#  Baa-Ba   0 0.09259259 0.1388889 0.33333333 0.3055556 0.1018519 0.02777778
#  Caa-Ba   0 0.00000000 0.2142857 0.07142857 0.2142857 0.3571429 0.14285714

#[[6]]
#       j
#hi      Aaa         Aa         A        Baa        Ba         B        Caa
#  A-B     0 0.00000000 0.2500000 0.33333333 0.2500000 0.1666667 0.00000000
#  Aa-B    0 0.20000000 0.2000000 0.40000000 0.2000000 0.0000000 0.00000000
#  Aaa-B NaN        NaN       NaN        NaN       NaN       NaN        NaN
#  B-B     0 0.05084746 0.1016949 0.06779661 0.2711864 0.3559322 0.15254237
#  Ba-B    0 0.04761905 0.1428571 0.17460317 0.2380952 0.2857143 0.11111111
#  Baa-B   0 0.12820513 0.1794872 0.23076923 0.2564103 0.1538462 0.05128205
#  Caa-B   0 0.04761905 0.0000000 0.09523810 0.1904762 0.5238095 0.14285714

#[[7]]
#         j
#hi        Aaa        Aa         A        Baa         Ba         B       Caa
#  A-Caa     0 0.0000000 0.0000000 0.33333333 0.66666667 0.0000000 0.0000000
#  Aa-Caa  NaN       NaN       NaN        NaN        NaN       NaN       NaN
#  Aaa-Caa NaN       NaN       NaN        NaN        NaN       NaN       NaN
#  B-Caa     0 0.0000000 0.0000000 0.05263158 0.26315789 0.4736842 0.2105263
#  Ba-Caa    0 0.0000000 0.1428571 0.21428571 0.14285714 0.2142857 0.2857143
#  Baa-Caa   0 0.1428571 0.0000000 0.00000000 0.42857143 0.2857143 0.1428571
#  Caa-Caa   0 0.0000000 0.0000000 0.06666667 0.06666667 0.4666667 0.4000000