[SOLVED] Mob not dissapearing on death

Yo!
So I made a function where it destroys the mob when dead so we can’t step on it when he is respawning.
But it doesn’t seem to work. With this code, we can still step on the mob’s body when he is currently respawning.

humanoid.Died:Connect(function()
    mob:Destroy()
end)

Why is it not working? This is a really simple code.

Is the .Died event even firing? maybe add a print

1 Like

Maybe it’s just because I’m a noob dev but it looks like a hard code for me :exploding_head: :rofl:

1 Like

Do you get any errors? I would also recommend checking what @bartkor12 said with the print.

Are you sure mob is also the character/mob?

1 Like

Yes. Because the first line in the event actually runs.

1 Like

Try checking if the Humanoid’s health reaches 0 instead of the died event.

Humanoid.HealthChanged:Connect(function()
         if Humanoid.Health <= 0 then
               mob:Destroy()
        end
end)
1 Like

If anyone wants the full code, I can provide it.

-- Services

local TS = game:GetService("TweenService")
local RS = game:GetService("ReplicatedStorage")

--------------------------------------------------------------------------------

-- Settings

local Settings = script.Parent:WaitForChild("Settings")

local RespawnTime = Settings:WaitForChild("RespawnTime").Value

local DeathAnimationID = "8674036190"

local Death_Infos = TweenInfo.new(2, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 0, false, 0)

--------------------------------------------------------------------------------

-- Variables

local NPC = script.Parent
local Dummy = NPC:Clone()
local Humanoid = NPC:WaitForChild("Humanoid")

local DeathAnimation = Instance.new("Animation")
DeathAnimation.AnimationId = "rbxassetid://"..DeathAnimationID
local DeathAnimationLoader = Humanoid:LoadAnimation(DeathAnimation)

local LeftFoot = NPC:WaitForChild("LeftFoot")
local RightFoot = NPC:WaitForChild("RightFoot")
local LeftLowerLeg = NPC:WaitForChild("LeftLowerLeg")
local LeftUpperLeg = NPC:WaitForChild("LeftUpperLeg")
local RightLowerLeg = NPC:WaitForChild("RightLowerLeg")
local RightUpperLeg = NPC:WaitForChild("RightUpperLeg")
local LowerTorso = NPC:WaitForChild("LowerTorso")
local UpperTorso = NPC:WaitForChild("UpperTorso")
local LeftLowerArm = NPC:WaitForChild("LeftLowerArm")
local LeftUpperArm = NPC:WaitForChild("LeftUpperArm")
local LeftHand = NPC:WaitForChild("LeftHand")
local RightLowerArm = NPC:WaitForChild("RightLowerArm")
local RightUpperArm = NPC:WaitForChild("RightUpperArm")
local RightHand = NPC:WaitForChild("RightHand")
local Head = NPC:WaitForChild("Head")
local MobGUI = Head:WaitForChild("MobGUI")
local Face = Head:WaitForChild("face")

local Tween1 = TS:Create(LeftFoot, Death_Infos, {Transparency = 1})
local Tween2 = TS:Create(RightFoot, Death_Infos, {Transparency = 1})
local Tween3 = TS:Create(LeftLowerLeg, Death_Infos, {Transparency = 1})
local Tween4 = TS:Create(LeftUpperLeg, Death_Infos, {Transparency = 1})
local Tween5 = TS:Create(RightLowerLeg, Death_Infos, {Transparency = 1})
local Tween6 = TS:Create(RightUpperLeg, Death_Infos, {Transparency = 1})
local Tween7 = TS:Create(LowerTorso, Death_Infos, {Transparency = 1})
local Tween8 = TS:Create(UpperTorso, Death_Infos, {Transparency = 1})
local Tween9 = TS:Create(LeftLowerArm, Death_Infos, {Transparency = 1})
local Tween10 = TS:Create(LeftUpperArm, Death_Infos, {Transparency = 1})
local Tween11 = TS:Create(LeftHand, Death_Infos, {Transparency = 1})
local Tween12 = TS:Create(RightLowerArm, Death_Infos, {Transparency = 1})
local Tween13 = TS:Create(RightUpperArm, Death_Infos, {Transparency = 1})
local Tween14 = TS:Create(RightHand, Death_Infos, {Transparency = 1})
local Tween15 = TS:Create(Head, Death_Infos, {Transparency = 1})
local Tween16 = TS:Create(script.Parent:WaitForChild("Hair"):WaitForChild("Handle"), Death_Infos, {Transparency = 1})

--------------------------------------------------------------------------------

-- No Climbing

script.Parent.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
script.Parent.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Seated, false)

--------------------------------------------------------------------------------

-- Respawn

Humanoid.Died:Connect(function(player)
	wait(RespawnTime)
	local Clone = Dummy:Clone()
	Clone.Parent = script.Parent.Parent
	Clone:MakeJoints()
	script.Parent:Destroy()
end)

--------------------------------------------------------------------------------

-- Death Animation

Humanoid.Died:Connect(function()
	DeathAnimationLoader:Play()
	NPC:WaitForChild("HumanoidRootPart").Anchored = true
	NPC:WaitForChild("HumanoidRootPart"):WaitForChild("Damage").Disabled = true
	wait(1.5)
	Tween1:Play()
	Tween2:Play()
	Tween3:Play()
	Tween4:Play()
	Tween5:Play()
	Tween6:Play()
	Tween7:Play()
	Tween8:Play()
	Tween9:Play()
	Tween10:Play()
	Tween11:Play()
	Tween12:Play()
	Tween13:Play()
	Tween14:Play()
	Tween15:Play()
	Tween16:Play()
	MobGUI.Enabled = false
	Face.Transparency = 1
	wait(RespawnTime)
	DeathAnimationLoader:Stop()
	MobGUI.Enabled = true
	Face.Transparency = 0
	NPC:WaitForChild("HumanoidRootPart").Anchored = false
end)

I will try this out later, thanks :slight_smile:

have you considered turning cancollide off

1 Like

Yes I did try turning CanCollide off for each bodypart.

After your problem is solved, please consider optimizing and organizing your code. There’s too many variables and boilerplate…

3 Likes

yes I was just thinking about it but I’m a little out of ideas to optimize this code. Could you point me in the right direction please? That would be really nice

1 Like

Sure. I can see that you have a variable and tween for every limb in the character. This can be done efficiently by instead iteraring the character’s children, and tweening that instead.

-- from:
local Tween1 = game.TweenService:Create(...
local Tween2 = game.TweenService:Create(...
local Tween3 = game.TweenService:Create(...
-- to:
for _, obj in pairs(NPC:GetDescendants()) do -- Iterates every descendant inside NPC
    if obj:IsA("BasePart") then
        game.TweenService:Create(obj, TweenInfo.new(2, Enum.EasingStyle.Exponential, Enum.EasingDirection.Out, 0, false, 0), 
            {Transparency = 1}:Play())
    end
end
4 Likes

Hi @bluebxrrybot thanks for your advice. We are going to try all that this afternoon. I also hope that we will find a solution with the characters that disappear but on which we can still walk until the moment of respawn

PS : i am @KrxKenDev 's father btw. We are trying to finalize a small game project together but KracKen (the scripterà is stuck on some scripts. It’s a shame because we are at about 80% of the project. But we try to go to the end all the same.

3 Likes

Reading through the post blind:

Debris = game:GetService("Debris")
humanoid.Died:Connect(function()
Debris:AddItem(mob, 5)
end)
1 Like

Try this:

humanoid.Died:Connect(function()
NPC:Destroy()
end)

1 Like

Hi all. Just wanted to thank you all for help and advices. The code is now optimized and the bug (we could walk on invisible parts of the dead mob until his respawn) is fixed. All seems working fine now. Thanks again !

2 Likes

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