How to stop animation on keybind pressed a second time

I am making a tool where you press a keybind and it plays an animation. But the thing is I want it so that if you press that keybind again it stops the animation. I have mostly everything figured out but the stop animation part. Without it even when you unequip the tool the animation keeps playing.

local tool = script.Parent
local anim = tool:FindFirstChild("Safety")
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local playAnim = humanoid:LoadAnimation(anim)


UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		playAnim:Play()
	end
end)

I have tried using

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		playAnim:Play()
	else
		playAnim:Stop()
		playAnim:Destroy()

but that just stops the animation after a few seconds
I have also tried putting the input again

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		playAnim:Play()
		if input.KeyCode == Enum.KeyCode.Q then
			playAnim:Stop()
			playAnim:Destroy()
		end
	end
end)

but that doesn’t play the animation at all so now I am pretty lost, I am pretty new to scripting and this is one of the first scripts I wrote without the help of a youtube tutorial so any help will be much appreciated

1 Like

it’s actually easy, you should add a boolean for example:

local bool = false

UserInputService.InputBegan:Connect(function(input)
      if input.KeyCode == Enum.KeyCode.Q and not bool then
          Animation:Play()
          bool = true
      elseif input.KeyCode == Enum.KeyCode.Q and bool then
          Animation:Stop()
          bool = false
     end
end)

or another way to avoid adding a bool is doing

UserInputService.InputBegan:Connect(function(input)
      if input.KeyCode == Enum.KeyCode.Q and not Animation.IsPlaying then
          Animation:Play()
      elseif input.KeyCode == Enum.KeyCode.Q and Animation.IsPlaying then
          Animation:Stop()
     end
end)
2 Likes

basically this script you’ve made checks if the user is pressing ANY button userinputservice and checks if the button is the Q, if yes it plays the animation, if it isn’t the Q it stops the animation, you can test it out and any button you press that is not Q will make the animation stop

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		playAnim:Play()
	else
		playAnim:Stop()
		playAnim:Destroy()

this one instead just checks if the player is pressing Q and plays the animation, after that it checks again if the player pressed Q and then it stops the animation, this is why the animation doesn’t play at all

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Q then
		playAnim:Play()
		if input.KeyCode == Enum.KeyCode.Q then
			playAnim:Stop()
			playAnim:Destroy()
		end
	end
end)

You can understand better the behaviours of the scripts by using print() after each line, to understand when they get triggered or stuff

2 Likes

Just a quick question, when you click q even though you dont have the tool equipped, the animation still plays. Is there a way to fix this?

Yeah, using the same logic, i don’t know how your script is structured but if it is inside the same script you can set a bool value to check if the item is equipped, for example when the player equips it the bool gets true, if not the bool turns off, and then add the bool to the check thing

1 Like

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