The Parent property of Gate is locked, current parent: NULL, new parent Gate1

So I am working at this script for a Gate that gets destroyed when its Health reaches 0. Everything works fine the first time the Gate reaches 0 Health. However the second time it reaches 0 Health the script breaks and it gives me the error message above.

local savegate = script.Parent.Gate:Clone()

while true do
	if script.Parent.GateHealth.Value <= 0 then
		script.Parent.Gate:Destroy()
		wait(10)
		savegate:Clone()
		savegate.Parent = script.Parent
		script.Parent.GateHealth.Value = 500
	end
	wait(1)
end

It’s because the 2nd time you try to destroy something that is already destroyed, when you destroy something, it parents it to nil, disconnects all connections, locks the parent property and does the same to all descendants.

From the looks of it, I think to solve your issue, you’d just need to put the new gate in a variable

local gate = script.Parent.Gate

local savegate = gate:Clone()

while true do
	if script.Parent.GateHealth.Value <= 0 then
		savegate:Destroy()
        savegate = nil
		wait(10)
		savegate = gate:Clone()
		savegate.Parent = script.Parent
		script.Parent.GateHealth.Value = 500
	end
	wait(1)
end

This doesnt seem to work as now the script seemed to duplicate the gate which has the result that there is now a destructible gate and an undestructible one.

By changing a bit how the gate gets stored I was able to eliminate the issue of having two gates. Here is for anyone else who might have similar problems the code

local gate = script.Parent.Gate
local savedgate = gate:Clone()
local save

while true do
	if script.Parent.GateHealth.Value <= 0 then
		script.Parent.Gate:Destroy()
		wait(10)
		save = savedgate:Clone()
		save.Parent = script.Parent
		script.Parent.GateHealth.Value = 500
	end
	wait(1)
end