How do I make an animation play and stop with the same input

This is my code so far

local Animation = script.CrouchAnim
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local track = hum:LoadAnimation(Animation)
local UIS = game:GetService("UserInputService")



UIS.InputBegan:Connect(function(input,proc)
	if proc then
		return
	end
	
  if input.KeyCode == Enum.KeyCode.E then
		
		track:Play()

end
	
	
	
end)

Can someone tell me how I can get it so if I press E once the animation plays and when I press it again it stops.

1 Like

IsPlaying

So how would I incorporate this in my script

Check if the animation is playing:

  • if playing then stop playing
  • else play
2 Likes

This just makes it that everytime I press w,s,a, or d it plays the animation then when I press w,s,a, or d again it stops the animation

local Animation = script.CrouchAnim
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local track = hum:LoadAnimation(Animation)
local UIS = game:GetService("UserInputService")



UIS.InputBegan:Connect(function(input,proc)
	if proc then
		return
	end
	
  if input.KeyCode == Enum.KeyCode.E then
		
		track:Play()
  end
  
  
if track.IsPlaying  then
	track:Stop()
else
	track:Play()
end
	
	
end)

1 Like

put it in the Enum.KeyCode.E???

1 Like
UIS.InputBegan:Connect(function(input,proc)
	if proc then
		return
	end

	if input.KeyCode == Enum.KeyCode.E then
		
		if track.IsPlaying then
			track:Stop()
		elseif not track.IsPlaying then	
			track:Play()
		end

	end
end)
1 Like

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