Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Animation for taking damage?

How could I make the player have to stop and play an animation when taking damage?

1 answer

Make sure this is in a localscript and is either placed in StarterCharacterScripts or StarterPlayerScripts.

I hope I've helped you out.

local Humanoid = game.Players.LocalPlayer.Character.Humanoid
local currentHealth = Humanoid.Health

local DamageAnimation = "rbxassetid://1764222386"

local function PlayAnimation(AnimationId)
	local Animation = Instance.new("Animation")
	Animation.AnimationId = AnimationId
	Animation.Name = AnimationId
	local LoadAnim = Humanoid:LoadAnimation(Animation)
	LoadAnim:Play()
end

local function StopAnimation(AnimationId)
	for index, child in pairs(Humanoid:GetPlayingAnimationTracks()) do
		if string.match(DamageAnimation, child.Name) then
			child:Stop()
		end
	end
end

Humanoid.HealthChanged:connect(function(health)
	local change = math.abs(currentHealth - health)
	if change >= 45 then
		PlayAnimation(DamageAnimation)
	end
	if health > 65 then
		StopAnimation(DamageAnimation)
	end
	currentHealth = health
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.