I am trying to make a script with 2 aminations that are toggled by a keybind, animation 1 plays on InputBegan and animation 2 plays on InputEnd. I want the player to be able to hold the keybind, holding the first animation until they decide to release it (InputEnd), so I used AdjustSpeed:(0)
to stop it at the end. Additionally, I want the second animation to play when the player releases the key, overriding the first animation.
It works fine until the end when the animation gets stuck in the position that would be at the end of animation 1.
I have a gif to demonstrate this issue.
https://i.gyazo.com/0611baaa870488ea0bfc0a3b58217d23.gif
The first animation plays when I hold the keybind, where I put my hands together, and then stops ( AdjustSpeed:(0)
) . The second animation, which takes me back to the starting position, plays when I release the key but afterwards the animation goes back to the previous position despite already being finished.
Note: Each animation is 2 seconds long hence the wait(2)
Code
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local HealStart = script.Animation1
local HealEnd = script.Animation2
local hold = false
UserInputService.InputBegan:Connect(function(input, isTyping)
if isTyping then return end
if hold == false then
if input.KeyCode == Enum.KeyCode.H then
local healTrack = player.Character.Humanoid.Animator:LoadAnimation(HealStart)
healTrack.Priority = Enum.AnimationPriority.Action
player.Character.Humanoid.WalkSpeed = 0
player.Character.Humanoid.JumpHeight = 0
healTrack:AdjustSpeed(1)
healTrack:Play()
wait(2)
healTrack:AdjustSpeed(0)
hold = true
end
end
end)
local hold = true
local HealStart = script.Animation1
local HealEnd = script.Animation2
UserInputService.InputEnded:Connect(function(input, isTyping)
if isTyping then return end
if hold == true then
if input.KeyCode == Enum.KeyCode.H then
local healTrack = player.Character.Humanoid.Animator:LoadAnimation(HealEnd)
healTrack.Priority = Enum.AnimationPriority.Action
healTrack:AdjustSpeed(1)
healTrack:Play()
wait(2)
player.Character.Humanoid.WalkSpeed = 16
player.Character.Humanoid.JumpHeight = 7.2
hold = false
end
end
end)```
Thank you for taking your time to read and help out.