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)