Door doesn't disappear after destroyed

Hello there,

Recently I added a feature to my door where there is a humanoid in it, and when the humanoid’s health is set to 0 the door is supposed to get destroyed, and the door’s transparency is set to 1, while the fallen door’s transparency is set to 0. But for some reason no matter how many times I hit the door it doesn’t seem to work. Does anyone know what I am doing wrong?

Code in my Door
local doorHumanoid = script.Parent
local DoorPart = script.Parent.Parent
local DoorPart2 = script.Parent.Parent
local DoorPart3 = script.Parent.Parent


game:GetService('Players').PlayerAdded:connect(function(player)
	player.CharacterAdded:connect(function(character)
		character:WaitForChild("Humanoid").Died:connect(function()
			doorHumanoid.Died:connect(function()
			DoorPart.Transparency = 1 
			DoorPart2.Transparency = 1
			DoorPart3.Transparency = 1

			wait(60 * 5) --Respawns the door)	
			DoorPart.Transparency = 0 
			DoorPart2.Transparency = 0
			DoorPart3.Transparency = 0
			
			
			end) 
		end)
	end)
end)
3 Likes

Change all of your :connect to :Connect, as it is the proper API reference.

Here is what your current code is doing:
For every player that joins the game, and every time their character re-spawns, you are making a new event every time their character dies, and every time their character dies, you are listening for the door humanoid to die - and when it does, you are setting the door parts transparencies to 1… then yielding for 5 minutes before setting back to 0.

from what you are explaining here:

Recently I added a feature to my door where there is a humanoid in it, and when the humanoid’s health is set to 0 the door is supposed to get destroyed, and the door’s transparency is set to 1, while the fallen door’s transparency is set to 0.

It sounds like you don’t need to have any of these player/character events at all… here is something that should do what you are saying.

doorHumanoid.Died:Connect(function()
    DoorPart.Transparency = 1
    DoorPart2.Transparency = 1
    DoorPart3.Transparency = 1

    DoorPart.CanCollide = false
    DoorPart2.CanCollide = false
    DoorPart3.CanCollide = false

    delay(60*5, function()
        doorHumanoid.Health = 100 -- or whatever
        DoorPart.Transparency = 0 
        DoorPart2.Transparency = 0
        DoorPart3.Transparency = 0
        DoorPart.CanCollide = false
        DoorPart2.CanCollide = false
        DoorPart3.CanCollide = false
    end)
end

NOTE:
This is a very hacky way to accomplish what you are trying to do… this can be done much cleaner like this

local main_door = script.Parent
local door_parts = {door1,door2,door3,etc} -- a list of your doors
local door_humanoid = main_door.Humanoid -- or wherever
local door_cframe = main_door.CFrame-- or main_door.PrimaryPart.CFrame, whatever you've got going on.

local function SpawnDoor()
    for i=1,#door_parts do
        local door_part = doors[i]
        door_part.Transparency = 0
        door_part.CanCollide = true -- or false, whatever you want it to be when it's alive.
    end
end

local function DoorDied()
    for i=1,#door_parts do
        local door_part = doors[i]
        door_part.Transparency = 1
        door_part.CanCollide = false
    end
    delay(60*5, SpawnDoor) -- in 5 minutes, (60*5/60) Spawn the door.
end

door_humanoid.Died:Connect(DoorDied)

Upon alteration of the variable references at the top, this should do what you are trying to accomplish.

As personal preference, I would prefer to :Destroy() and :Clone() the door on death/spawn. However the supplied method wouldn’t hurt performance or anything, so it’s fine.

1 Like

The Died event is bad and doesn’t fire unless the humanoid is “proper” (i.e. has a head/etc). You’ll need to listen for HealthChanged and check if Health is equal to 0.

3 Likes

Thanks for responding to my question!

I am having a bit of a problem with it, it still doesn’t become transparent after it is destroyed.

Would I need to define “doors” as

local doors = Script.Parent.Parent

Also this is where the DoorParts are located inside my door.
image

1 Like

from what I can see, door_parts would be defined like this:

local main_door = script.Parent -- the main model
 -- you could use :GetChildren() and add an if statement to the for loops to check that it's a basepart (if door_part:IsA("BasePart") then change transparencies/cancollide end)
local door_parts = main_door.Detective:GetChildren()   

--OR do it manually.
local door_parts = {
    main_door.Detective.DoorPart;
    main_door.Detective.DoorPart2;
    main_door.Detective.DoorPart3;
    main_door.Detective; -- and the detecter part too, if you need to.
}

also, @EchoReaper is right. Change the .Died event to

door_humanoid.HealthChanged:Connect(function()
    if door_humanoid.Health <= 0 then
        DoorDied()
    end
end)
1 Like