Breakable Wall only respawned once (Parent Property Lock)

The wall only respawned once and then gives out Parent Property Lock error

How the system works, or atleast how it should:

In Fireball script:
Fire Ball >> Detects Wall >> Apply Debris To Wall

In Wall script:
Wall Detects Remove Childrem >> Respawn >> Repeat

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)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.