I've made a `LocalScript` for Death Animations. However, The animation was only working when the player either reset themselves or their health was set to 0 and less.
When the player get attacks by, e.g. Enemy NPC.
So far, I have tried out many solutions and I still couldn't get a firm grasp of the issues
This is the current code:
local Players = game:GetService("Players")
local Rep = game:GetService("ReplicatedStorage")
local BlackScreenModule = require(Rep.Module:WaitForChild("BlackScreen")) --Black Screen Module
local player = Players.LocalPlayer
---------------------------------Death Animation----------------------------------------
player.CharacterAdded:Connect(
function(char)
Players.CharacterAutoLoads = false -- This disables the automatic 5 seconds respawning
local Hum = char:WaitForChild("Humanoid")
local Animator = Hum:WaitForChild("Animator")
Hum.BreakJointsOnDeath = false
Hum:SetStateEnabled(Enum.HumanoidStateType.Dead, false)--Disable Enum.HumanoidStateType.Dead, This will prevent the character from falling apart upon death.
local DeathAnim = Instance.new("Animation")
DeathAnim.AnimationId = "rbxassetid://13111865980" --Death Anim
--Load the animation onto the animator
local DeathAnimationTrack = Animator:LoadAnimation(DeathAnim)
--When Player's HP reached 0 it will play the animation with death ui
Hum.HealthChanged:Connect(
function()
if Hum.Health <= 0 then --Check if Health <= 0 and then plays animation if so.
player.Character.HumanoidRootPart.Anchored = true --Stops player from moving
local PlayerDeadState = Rep.Value:FindFirstChild("PlayerDead")
PlayerDeadState.Value = true ---Set player state to dead (DEFAULT: FALSE)
DeathAnimationTrack:Play() --plays anim
task.wait(2)
BlackScreenModule.UIdeath() --Enable Game over screen
end
end
)
end
)
----------------------------------------------------------------------------------------------
Here is a few examples:
Resetting
Enemy NPC
Note: I have also tried out variety of the NPC from toolbox. And none of them seems to be working as well.
local Players = game:GetService("Players")
local Rep = game:GetService("ReplicatedStorage")
local BlackScreenModule = require(Rep.Module:WaitForChild("BlackScreen")) --Black Screen Module
local player = Players.LocalPlayer
---------------------------------Death Animation----------------------------------------
player.CharacterAdded:Connect(
function(char)
Players.CharacterAutoLoads = false -- This disables the automatic 5 seconds respawning
local Hum = char:WaitForChild("Humanoid")
local Animator = Hum:WaitForChild("Animator")
Hum.BreakJointsOnDeath = false
Hum:SetStateEnabled(Enum.HumanoidStateType.Dead, false)--Disable Enum.HumanoidStateType.Dead, This will prevent the character from falling apart upon death.
local DeathAnim = Instance.new("Animation")
DeathAnim.AnimationId = "rbxassetid://13111865980" --Death Anim
--Load the animation onto the animator
local DeathAnimationTrack = Animator:LoadAnimation(DeathAnim)
--When Player's HP reached 0 it will play the animation with death ui
Hum.HealthChanged:Connect(
function()
if Hum.Health <= 0 then --Check if Health <= 0 and then plays animation if so.
player.Character.HumanoidRootPart.Anchored = true --Stops player from moving
local PlayerDeadState = Rep.Value:FindFirstChild("PlayerDead")
PlayerDeadState.Value = true ---Set player state to dead (DEFAULT: FALSE)
DeathAnimationTrack:Play() --plays anim
task.wait(2)
BlackScreenModule.UIdeath() --Enable Game over screen
end
end
)
end
)
----------------------------------------------------------------------------------------------
Add this to your function.
Humanoid.Died function will fires when the character died.
Hum.Died:Connect(
function()
player.Character.HumanoidRootPart.Anchored = true --Stops player from moving
local PlayerDeadState = Rep.Value:FindFirstChild("PlayerDead")
PlayerDeadState.Value = true ---Set player state to dead (DEFAULT: FALSE)
DeathAnimationTrack:Play() --plays anim
task.wait(2)
BlackScreenModule.UIdeath() --Enable Game over screen
end
)
Tried this and it refused to work completely. It seems like it’s not registering humanoid death.
Am I doing this wrong?
player.CharacterAdded:Connect(
function(char)
Players.CharacterAutoLoads = false -- This disables the automatic 5 seconds respawning
local Hum = char:WaitForChild("Humanoid")
local Animator = Hum:WaitForChild("Animator")
Hum.BreakJointsOnDeath = false
Hum:SetStateEnabled(Enum.HumanoidStateType.Dead, false)--Disable Enum.HumanoidStateType.Dead, This will prevent the character from falling apart upon death.
local DeathAnim = Instance.new("Animation")
DeathAnim.AnimationId = "rbxassetid://13111865980" --Death Anim
--Load the animation onto the animator
local DeathAnimationTrack = Animator:LoadAnimation(DeathAnim)
--When Player died, it will play the animation with death ui
Hum.Died:Connect(
function()
player.Character.HumanoidRootPart.Anchored = true --Stops player from moving
local PlayerDeadState = Rep.Value:FindFirstChild("PlayerDead")
PlayerDeadState.Value = true ---Set player state to dead (DEFAULT: FALSE)
DeathAnimationTrack:Play() --plays anim
task.wait(2)
BlackScreenModule.UIdeath() --Enable Game over screen
end
)
end
)
----------------------------------------------------------------------------------------------
I fixed it by removing the “Enum.HumanoidStateType.Dead” line that prevents player from dying.
However it’s still only works upon resetting and not by Enemy NPC apparently?
For those who are reading, I have managed to fix this issues.
From my guessing,
I think it was because NPC uses a normal script to damage the player. But I used localscript to set the humanoid breakjoints to false.
Even though in the serverside. server will still sees the humanoid breakjoints as true.
So I removed the breakjoints line on my localscript and moved it into serverside instead.
ServerScriptService:
--Assist script for Death Script
local PLR = game:GetService("Players")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)
local Humanoid = char:WaitForChild("Humanoid")
Humanoid.BreakJointsOnDeath = false --Disable player falling apart on death (This make animation works)
PLR.CharacterAutoLoads = false -- This disables the automatic 5 seconds respawning
end)
end)
StarterPlayerScripts:
local Players = game:GetService("Players")
local Rep = game:GetService("ReplicatedStorage")
local BlackScreenModule = require(Rep.Module:WaitForChild("BlackScreen")) --Black Screen Module
local player = Players.LocalPlayer
---------------------------------Death Animation----------------------------------------
player.CharacterAdded:Connect(
function(char)
local Hum = char:WaitForChild("Humanoid")
local Animator = Hum:WaitForChild("Animator")
local DeathAnim = Instance.new("Animation")
DeathAnim.AnimationId = "rbxassetid://13111865980" --Death Anim
--Load the animation onto the animator
local DeathAnimationTrack = Animator:LoadAnimation(DeathAnim)
--When Player died, it will play the animation with death ui
Hum.Died:Connect(
function()
player.Character.HumanoidRootPart.Anchored = true --Stops player from moving
local PlayerDeadState = Rep.Value:FindFirstChild("PlayerDead")
PlayerDeadState.Value = true ---Set player state to dead (DEFAULT: FALSE)
DeathAnimationTrack:Play() --plays anim
task.wait(2)
BlackScreenModule.UIdeath() --Enable Game over screen
end
)
end
)
----------------------------------------------------------------------------------------------