Block animation continues to player after player lets go of key?

I’d like the player to play the blocking animations for as long as they hold down the popper keys, but when you let go of the keys the animation stops mid air and won’t go back down until the player moves again. I’ve tried just turning off the loop and playing the animation 1 last time to get the player’s arm down but it didn’t work.

local RightPunchEvent = game.ReplicatedStorage:WaitForChild("RightPunch")
local LeftPunchEvent = game.ReplicatedStorage:WaitForChild("LeftPunch")
local RightBlockEvent = game.ReplicatedStorage:WaitForChild("RightBlock")
local LeftBlockEvent = game.ReplicatedStorage:WaitForChild("LeftBlock")

local isBlockingLeft = false
local isBlockingRight = false

local function playAnimation(player, animationId)
    local humanoid = player.Character and player.Character:FindFirstChildOfClass("Humanoid")
    if humanoid then
        local animator = humanoid:FindFirstChild("Animator")
        if animator then
            local animation = Instance.new("Animation")
            animation.AnimationId = "rbxassetid://" .. animationId
            local track = animator:LoadAnimation(animation)
            track:Play()
            track.Looped = true
            return track
        end
    end
    return nil
end

local function stopAnimation(track)
    if track then
        track:Stop()
        track:Destroy()
    end
end

local function onKeyPress(input)
    local player = game.Players.LocalPlayer
    if input.KeyCode == Enum.KeyCode.E then
        RightPunchEvent:FireServer()
    elseif input.KeyCode == Enum.KeyCode.Q then
        LeftPunchEvent:FireServer()
    elseif input.KeyCode == Enum.KeyCode.U and not isBlockingLeft then
        isBlockingLeft = true
        local track = playAnimation(player, 15479354647)
        if track then
            track.Stopped:Connect(function()
                isBlockingLeft = false
            end)
        end
        LeftBlockEvent:FireServer()
    elseif input.KeyCode == Enum.KeyCode.J and not isBlockingRight then
        isBlockingRight = true
        local track = playAnimation(player, 15484414696)
        if track then
            track.Stopped:Connect(function()
                isBlockingRight = false
            end)
        end
        RightBlockEvent:FireServer()
    end
end

local function onKeyRelease(input)
    local player = game.Players.LocalPlayer
    if input.KeyCode == Enum.KeyCode.U and isBlockingLeft then
        stopAnimation(player.Character:FindFirstChildOfClass("Humanoid").Animator:FindFirstChild(tostring(15479354647)))
    elseif input.KeyCode == Enum.KeyCode.J and isBlockingRight then
        stopAnimation(player.Character:FindFirstChildOfClass("Humanoid").Animator:FindFirstChild(tostring(15484414696)))
    end
end

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("UserInputService").InputEnded:Connect(onKeyRelease)

It might be caused by destroying the AnimationTrack, making it unable to move the players arms back. Try to remove it

Removing this just breaks the animation entirely.

I mean don’t destroy the AnimationTrack after stopping it, sorry if it didn’t make sense to you

You will need to measure the length/duration of your animation and you will have to hold record of how long the client has hold down the key. Then, when he stops to press it down you need to subtract the time that the last replayed animation already played and send it over to the stopAnimation() function. The rest is history:

local function stopAnimation(track, duration)
    if track then
        task.wait(duration)
        track:Stop()
        track:Destroy()
    end
end
1 Like

Extremely helpful thank you. @kwkxbxkdkdjjd @neweve2323

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