构建脚本不是 运行
Build script not running
我正在尝试制作构建脚本,但遇到了问题。我的函数 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)
您似乎正试图在已销毁且不再存在的实例上调用方法。试试这个代码,如果有效,请确保将我标记为解决方案!:
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
我正在尝试制作构建脚本,但遇到了问题。我的函数 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)
您似乎正试图在已销毁且不再存在的实例上调用方法。试试这个代码,如果有效,请确保将我标记为解决方案!:
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