How do make a function work until a specific part has been removed

So I have two doors which has the same key card and when one of the doors gets destroyed the other one will also be destroyed.

Here is the script that I used

if game.Workspace.HammerBasement.MainDoor == nil then
	script.Parent:Destroy()
end

I don’t why this doesn’t work this once worked for me.

Can anyone help? :frowning:

Put both the doors in a model and then delete the model of the doors

1 Like

Thanks but here is the problem -

Explorer..
I want the main door to get destroyed when both the planks are destroyed.

I know how to destroy the planks but the main doesn’t destroyed when both planks are destroyed.

Can you help? :frowning:

Maybe putting this code in a script inside the HammerBasement model would work.

local basement = script.Parent
local planksDestroyed = 0
local conn
conn = basement.ChildRemoved:Connect(function(child)
    If child.Name:match("Plank") then
        planksDestroyed += 1
        if planksDestroyed == 2 then
            basement.MainDoor:Destroy()
            basement.BasementExit:Destroy()
            conn:Disconnect()
        end
    end
end)
2 Likes

Ok thanks I will check it. :smiley:

Try this:

game.Workspace.HammerBasement.ChildRemoved:Connect(function()
      if not game.Workspace.HammerBasement:FindFirstChild("MainDoor") then
       script.Parent:Destroy()
     end
end)

This basically fires whenever a Child gets removed.
Also, your code would error and it would give you a message along those lines:
“Line 1 - MainDoor is not a valid member of HammerBasement” as it doesn’t exist, it got removed.
So you have to use FindFirstChild, as it returns nil if it can’t find the child instead of erroring.