How to execute player animation on death/reset?

Hi there! I made an animation for an R15 rig which I’d like any player ingame to be able to execute upon resetting or dying. Was wondering if anyone knew how to script that and additionally make it so the full length of the animation is able to play before the player respawns. It’s only about 1:40 seconds long.

(I can’t script so please explain it in basic terms for me if you can thanks lol)

Any help is much appreciated!

2 Likes

Hope this properly guides you.

local Players =  game:GetService("Players")

--// Initialization//--
Players.CharacterAutoLoads = false -- This disables the automatic 5 seconds respawning

-- Wait for the player to join the game
Players.PlayerAdded:Connect(function(player)
    -- Runs the code inside and hooks the effect whenever the character spawns again
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        local animator = humanoid:WaitForChild("Animator")

        -- Disable body parts "exploding" upon death to properly play the animation
        humanoid.BreakJointsOnDeath = false
        
        -- Creates the animation
        local animation = Instance.new("Animation")
        animation.AnimationId = "rbxassetid://AnimationIdHere"
        local animationTrack = animator:LoadAnimation(animation)

        -- Listens for the death of the humanoid
        local diedConnection
        diedConnection = humanoid.Died:Connect(function()
            diedConnection:Disconnect() -- Prevent multi-running/Potential memory leaks
            animationTrack:Play() -- Plays the animation
            animationTrack.Stopped:Wait() -- Waits for the animation to stop
            animationTrack:Destroy() -- Destroys both animation and track for potential memory leaks
            animation:Destroy()
            player:LoadCharacter() -- Spawns character
        end)
    end)
end)
3 Likes

Hey so this script seems to have locked my camera in place and doesn’t spawn my character in.

Oops, sorry about that!

local Players =  game:GetService("Players")

--// Initialization//--
Players.CharacterAutoLoads = false -- This disables the automatic 5 seconds respawning

-- Wait for the player to join the game
Players.PlayerAdded:Connect(function(player)
	-- Runs the code inside and hooks the effect whenever the character spawns again
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		local animator = humanoid:WaitForChild("Animator")
		
		-- Wait until humanoid exists on the workspace/player character
		repeat
			wait()
		until humanoid.Parent == character
		
		-- Disable body parts "exploding" upon death to properly play the animation
		humanoid.BreakJointsOnDeath = false

		-- Creates the animation
		local animation = Instance.new("Animation")
		animation.AnimationId = "rbxassetid://AnimationIdHere"
		local animationTrack = animator:LoadAnimation(animation)

		-- Listens for the death of the humanoid
		local diedConnection
		diedConnection = humanoid.Died:Connect(function()
			diedConnection:Disconnect() -- Prevent multi-running/Potential memory leaks
			animationTrack:Play() -- Plays the animation
			animationTrack.Stopped:Wait() -- Waits for the animation to stop
			animationTrack:Destroy() -- Destroys both animation and track for potential memory leaks
			animation:Destroy()
			player:LoadCharacter() -- Spawns character
		end)
	end)
	
	-- Loads the character for the first time
	player:LoadCharacter()
end)

2 changes: Because of the behavior of character spawning, we have to wait until the Humanoid exists within the physical realm first.
After creating the CharacterAdded event (very important to do this first), we spawn the character for the first time (which I completely forgot to do :stuck_out_tongue:)

3 Likes

Thanks for the fix haha. The animation executes upon resetting but it looks like gravity or some other external factor pulls the character backwards so I fall over the wrong way while the animation is playing.

Ah, went over my head. Add this right after the diedConnection:Disconect() line:

if character.HumanoidRootPart then -- Checks just in case player jumps in the void
    character.HumanoidRootPart.Anchored = true
end
7 Likes

Thanks a ton you’re a huge help! :slight_smile:

This won’t work like you think it does. If character does not have a HumanoidRootPart, trying to index character for it will error. Use FindFirstChild if you want to check whether something exists or not.

3 Likes

Yeah, another mistake on my end. Typically, I would just group it up with the humanoid and animator variables… not to mention I’ve been working heavily with tables for the past month so the validity of direct indexing totally messed with me haha.

@nd89 I would replace the HumanoidRootPart code with this:

local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
    humanoidRootPart.Anchored = true
end
2 Likes

Ah sorry just saw this. Thank you guys very much haha