Animation not stopping

  1. What do you want to achieve? Fixing the problem.

  2. What is the issue? After I press T (The keybind I’ve set it to) then pressing it again the animation doesn’t stop.

  3. What solutions have you tried so far? All solutions I’ve found.

UserInputService.InputBegan:Connect(function(Key, IsTyping)
	if IsTyping then return end
	if Player.Team.TeamColor == BrickColor.new("Lapis") then
		if Key.KeyCode == Enum.KeyCode.T then
			ChangeActive()
			local Player = game.Players.LocalPlayer
			local Character = Player.Character
			local Humanoid = Character:WaitForChild("Humanoid")
			local AnimationTrack = Humanoid:LoadAnimation(Animation)

			if not AnimationTrack.IsPlaying then
				AnimationTrack:Play()
			else
				AnimationTrack:Stop()
			end
		end
	end
end)

Because the animation in the InputBegan function is newly created one which means every time you press T you’re playing the same animation over and over instead of stopping it.

I wouldn’t suggest relying your debounce on your AnimationTrack’s playing status. Instead, I would use a variable with a bool value, checking if the person is running or not.

local runningVariable = false

UserInputService.InputBegan:Connect(function(Key, IsTyping)
	if IsTyping then return end
	if Player.Team.TeamColor == BrickColor.new("Lapis") then
		if Key.KeyCode == Enum.KeyCode.T then
			ChangeActive()
			local Player = game.Players.LocalPlayer
			local Character = Player.Character
			local Humanoid = Character:WaitForChild("Humanoid")
			local AnimationTrack = Humanoid:LoadAnimation(Animation)

			if runningVariable == false then
				AnimationTrack:Play()
			else
				AnimationTrack:Stop()
			end
		end
	end
end)

Hope this helped.

That’s nice to know, but how would I fix it though?

Sadly that didn’t work for me.

You could either set the Humanoid:LoadAnimation(Animation) outside of the InputBegin function or just like @Sensei_Developer suggested make a variable that detects if you’ve already press T.

I’ve also use GetPlayingAnimationTracks to get the animation that is currently running and check if they’re the same as the one you want to stop.

UserInputService.InputBegan:Connect(function(Key, IsTyping)
	if IsTyping then return end
	if Player.Team.TeamColor == BrickColor.new("Lapis") then
	if Key.KeyCode == Enum.KeyCode.T then
		ChangeActive()
		local Player = game.Players.LocalPlayer
		local Character = Player.Character
		local Humanoid = Character:WaitForChild("Humanoid")
		local AnimationTrack = Humanoid:LoadAnimation(Animation)

		if not runningVariable then
			runningVariable = true
			AnimationTrack:Play()
		else
			runningVariable = false
			for _, GetAnimation in pairs(Humanoid:GetPlayingAnimationTracks()) do
				if GetAnimation.Name == Animation.Name then
					GetAnimation:Stop()
				end
			end
		end
	end
end)

I get a syntax error at the end) saying the ) shouldn’t be there

My bad I’m so sorry. Here is the fixed version of the code.

UserInputService.InputBegan:Connect(function(Key, IsTyping)
	if IsTyping then return end
	if Player.Team.TeamColor == BrickColor.new("Lapis") then
		if Key.KeyCode == Enum.KeyCode.T then
			ChangeActive()
			local Player = game.Players.LocalPlayer
			local Character = Player.Character
			local Humanoid = Character:WaitForChild("Humanoid")
			local AnimationTrack = Humanoid:LoadAnimation(Animation)

			if not runningVariable then
				runningVariable = true
				AnimationTrack:Play()
			else
				runningVariable = false
				for _, GetAnimation in pairs(Humanoid:GetPlayingAnimationTracks()) do
					if GetAnimation.Name == Animation.Name then
						GetAnimation:Stop()
					end
				end
			end
		end
	end
end)

Thanks for the help it works now!