Animation Won't Play After Death

I have a script, everything works, but whenever the player dies the animation just stops playing.

–The Script

game.Players.PlayerAdded:Connect(function (plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local hum = char.Humanoid
	if hum then
		local animator = hum:FindFirstChild("Animator")
		local animTrack = animator:LoadAnimation(game.ReplicatedStorage.Animations.Running)
		local animTrack2 = animator:LoadAnimation(game.ReplicatedStorage.Animations.WeaponRun)
		local C = coroutine.create(function ()
			while task.wait(0.1)  do
				print(hum.WalkSpeed)
				if hum then
					if hum.WalkSpeed > 16 and not hum.Sit and not char:FindFirstChild("Tool") then
						if animTrack.IsPlaying == false then
							animTrack:Play()
							animTrack:AdjustSpeed(2)
						end
					elseif hum.WalkSpeed > 16 and not hum.Sit and char:FindFirstChild("Tool") then
						if not animTrack2.IsPlaying then
							animTrack2:Play()
							animTrack2:AdjustSpeed(2)
						end
					else
						animTrack:Stop()
						animTrack2:Stop()
					end
				end
			end
		end)

		coroutine.resume(C)
	end
end)
1 Like

I think you need to set the humanoid again once it dies since it gets replaced by a new one.

Like what pink sheep said, the original humanoid the player joined the game with is destroyed and replaced with a new one when the player dies, so something like this might work better instead;

game.Players.PlayerAdded:Connect(function (plr)
	plr.CharacterAdded:Connect(function(char)
		local hum = char.Humanoid
		if hum then
			local animator = hum:FindFirstChild("Animator")
			local animTrack = animator:LoadAnimation(game.ReplicatedStorage.Animations.Running)
			local animTrack2 = animator:LoadAnimation(game.ReplicatedStorage.Animations.WeaponRun)
			local C = coroutine.create(function ()
				while task.wait(0.1)  do
					print(hum.WalkSpeed)
					if hum then
						if hum.WalkSpeed > 16 and not hum.Sit and not char:FindFirstChild("Tool") then
							if animTrack.IsPlaying == false then
								animTrack:Play()
								animTrack:AdjustSpeed(2)
							end
						elseif hum.WalkSpeed > 16 and not hum.Sit and char:FindFirstChild("Tool") then
							if not animTrack2.IsPlaying then
								animTrack2:Play()
								animTrack2:AdjustSpeed(2)
							end
						else
							animTrack:Stop()
							animTrack2:Stop()
						end
					end
				end
			end)

			coroutine.resume(C)
		end
	end)
end)

The ‘CharacterAdded’ event fires whenever the player spawns or respawns, it fires as soon as the Player.Character value is no longer nil. You can get the current humanoid instance the player is using through this.

2 Likes

thanks for the help adjusted some of the script to fit what you guys were talking about.

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