Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Animation for taking damage?

Asked by 6 years ago

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

1 answer

Log in to vote
0
Answered by 6 years ago

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)
Ad

Answer this question