How do I make a animation stop playing when the keybind is hit again

I want to make an animation to play when you press F, but if you press it again the animation stops.
How can I do this?

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
	if key == "f" then
		local db = false
		local hum = script.Parent:WaitForChild("Humanoid")
		local humanim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))
		humanim:Play()
		humanim.Looped = true
		end
end)
3 Likes

Put this in a Local script inside of StarterCharacterScripts
image


local animplaying = false

repeat wait() until script.Animation  -- Change to Name of Animation 

local hum = script.Parent:WaitForChild("Humanoid")

local humanim = hum:LoadAnimation(script.Animation) -- Change to Name of Animation 

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
	if key == "f" then
		if animplaying == false then
			animplaying = true
			humanim:Play()
			humanim.Looped = true
		else
			animplaying = false
			humanim:Stop()
		end
	end
end)
3 Likes

It randomly stops the animation, Is there something I need to do?

Maybe make the animation looped. I tested it with a looped animation that I made and it worked with no problems. But then again the “humanim.Looped = true” should have fixed that.

I tried that. Not working, what do I do?

game.Players.LocalPlayer:GetMouse().KeyDown:connect(function(key)
	if key == "f" then
if db then
		local db = false
		local hum = script.Parent:WaitForChild("Humanoid")
		local humanim = hum:LoadAnimation(script:FindFirstChildOfClass("Animation"))
		humanim:Play()
		humanim.Looped = true
		end
else if not db then
humananim:stop()
end




end)

This doesn’t work and doesn’t solve the randomly stopping problem.

Hey, I recommend you to use UserInputService for good practice. Anyways, you should use a variable to determine if the animation is playing, and stop the animation if the variable is true.

local UIS = game:GetService('UserInputService')
local Key = 'F'
local Animation, AnimationPlaying = nil, false

UIS.InputBegan:Connect(function(Input, UI_Interacting)
    if Input.KeyCode == Enum.KeyCode[Key] then
        if AnimationPlaying then
            Animation:Stop() -- This might not be correct.
        else
            AnimationPlaying = true
            Animation:Play()
            Animation.Stopped:Wait()
            AnimationPlaying = false
            -- Start animation
        end
    end
end)

This should work…

Attempt to index nil with Stop() is the error it prints.

You need to assign Animation to the AnimationTrack (an Instance returned by LoadAnimation).

I’m kinda bad at animating, I’ll try this. Thanks.

It says animation is not a valid member of keybind, even tho it is?

local UIS = game:GetService('UserInputService')
local Key = 'F'
local Animation, AnimationPlaying = nil, false
local hum = script.Parent:WaitForChild("Humanoid")
local animation = hum:LoadAnimation(script.Animation) -- error occurs here
UIS.InputBegan:Connect(function(Input, UI_Interacting)
	if Input.KeyCode == Enum.KeyCode[Key] then
		if AnimationPlaying then
			animation:Stop() -- This might not be correct.
		else
			AnimationPlaying = true
			animation:Play()
			animation.Stopped:Wait()
			AnimationPlaying = false
			-- Start animation
		end
	end
end)

Edit: Try adding print statements wherever possible, like this:

local UIS = game:GetService('UserInputService')
local Key = 'F'
local animation, AnimationPlaying = nil, false
local hum = script.Parent:WaitForChild("Humanoid")
local animation = hum:LoadAnimation(script.Animation)

UIS.InputBegan:Connect(function(Input, UI_Interacting)
	if Input.KeyCode == Enum.KeyCode[Key] then
        print('Key pressed.')

		if AnimationPlaying then
            print('Animation was playing. Stopping animation.')
			animation:Stop() -- This might not be correct.
		else
            print('Playing animation.')
			AnimationPlaying = true
			animation:Play()
			animation.Stopped:Wait()
			AnimationPlaying = false
			-- Start animation
		end
	end
end)

I thought it was the problem but the problem presists.

2 Likes