local model = script.Parent
for _, v in pairs(model:GetChildren()) do
if v:IsA("MeshPart") then
local cell = v:Clone()
local oriCf = cell.CFrame
model.ChildRemoved:Connect(function()
cell.CFrame = oriCf
cell.Parent = model
end)
end
end
The original parts get destroyed, then the replacement parts are put in where the original parts are, but then, when it’s destroyed again, the replacement parts are destroyed as the replacement parts try to be parented back to the wall (resulting in the parent locked error). You’d have to make new replacement parts each time the wall was destroyed. Try something like this:
local model = script.Parent
for _, v in pairs(model:GetChildren()) do
if v:IsA("MeshPart") then
local cell = v:Clone()
local oriCf = cell.CFrame
model.ChildRemoved:Connect(function()
cell.CFrame = oriCf
cell.Parent = model
cell = cell:Clone()
oriCf = cell.CFrame
end)
end
end
(Sorry I wrote this on mobile I can fix the code if it doesn’t work or has issues later)
Thanks man! Initially the updated script freezes the game. But, adding task.wait() fixed it! It works perfectly now.
local model = script.Parent
for _, v in pairs(model:GetChildren()) do
if v:IsA("MeshPart") then
local cell = v:Clone()
local oriCf = cell.CFrame
model.ChildRemoved:Connect(function()
cell.CFrame = oriCf
cell.Parent = model
task.wait()
cell = cell:Clone()
oriCf = cell.CFrame
end)
end
end
That’s strange. Just make sure it doesn’t do this too much as that creates a lot of unnecessary parts. That could have to do with the fireball hitting the wall. (If that is the case, you can add a cooldown to when the fireball touches something, or destroy the fireball on impact)