I am currently working on a crawling script for my game. I have a LocalScript in StarterPack with an Animation in the LocalScript. In my code, it starts playing the animation when Z is pressed once, but when it is pressed a second time it should stop but doesn't. In the output, it doesn't print ("Z was pressed again") after it's pressed a second time. The animation is looped, is in R15, and the priority is action. Here is what I currently have.
function KeyPress(input, gameProcessed)
local pressed = 0
local animation
game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Animation)
if input.KeyCode == Enum.KeyCode.Z then
pressed = 1
print("Z was pressed")
animation:Play()
else
if input.KeyCode == Enum.KeyCode.Z and pressed == 1 then
pressed = 0
animation:Stop()
print("Z was pressed again")
end
end
end
game:GetService("UserInputService").InputBegan:Connect(KeyPress)
If anybody knows why this is not working it would be greatly appreciated if you answered.

2 comments
The issue I see with your script is that you are declaring a local variable
pressedeach time the button is pressed. It is being assigned to 0 each time. It should be moved outside of the function.Now it prints ("Z was pressed again"), but still doesn't stop the animation.