How to Toggle an Animation on and off with one key?

Hi, my script is not working, it plays the animation, but when I press it again, it just doesn’t stop. and I’m wondering how I would make the animation play if you pressed Y the first time and if you press y again the animation stops?

If you know how, please tell me.

Could we see the code, that might help.

You can try do this:

for _, anim in pairs(humanoid:GetPlayingAnimationTracks()) do
	if anim == "(Animation name you need to stop)" then
		anim:Stop()
	end
end

Hope I helped you!

1 Like

@Datsun4885 Hes my script

local plr = game.Players.LocalPlayer
local Key = game:GetService("UserInputService")
local char = plr.Character
local Frame = game:GetService('StarterGui').GUI.Frame

if not char then
	plr.CharacterAdded:Wait()
end

local hum = char:WaitForChild('Humanoid')

local animation = Instance.new("Animation")

animation.AnimationId = "rbxassetid://86672075079690"

local animationtrack = hum:LoadAnimation(animation)

function playAnimation()
	animationtrack:Play()
end

function StopAnimation()
	animationtrack:Stop()
end

Key.InputBegan:Connect(function(input)
	if Frame.Visible == false then
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.F then
				playAnimation()
			else
				StopAnimation()
			end
		end
	end	
end)

Thank you, you Fixed my script.