Problem when using the same key to start and stop an animation

I am making a gun and am inplementing a crouch system to the gun.

The goal is to click the same key to start the animation, and let it play and then when the user is ready stop it clickes the same key to stop the animation.

This is my script: Local Script

local animation = script:WaitForChild("Crounch")
local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local char = player.Character
local humanoid = char:WaitForChild("Humanoid")

local tool = script.Parent

local crouch = humanoid:LoadAnimation(animation)
crouch.Priority = Enum.AnimationPriority.Action
crouch.Looped = true

tool.Equipped:Connect(function()
	UIS.InputBegan:Connect(function(input, gameProccessed)
		if gameProccessed then		
			if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.C then		
				crouch:Play()
				print("started")		
			end		
		end	
	end)
end)

tool.Equipped:Connect(function()
	UIS.InputBegan:Connect(function(input, gameProccessed)
		if gameProccessed then
			if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.C then
				crouch:Stop()
				print("stopped")
			end
		end
	end)
end)

tool.Unequipped:Connect(function()
	crouch:Stop()
end)

--blah blah referencing variables
local isPlayingAnim = false
tool.Equipped:Connect(function()
	UIS.InputBegan:Connect(function(input, gameProcessed)
		if gameProcessed then
			if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.C then
				if isPlayingAnim == false then
					crouch:Play()
					print("Started")
					isPlayingAnim = true
				end
			end
		end
		
	end)
end)

tool.Unequipped:Connect(function()
	crouch:Stop()
	isPlayingAnim = false
end)

crouch.Stopped:Connect(function()
	isPlayingAnim = false
end)

ok lemme try this and thanks for the contribution

If there are any errors tell me I’ll try to fix the code.

it didnt print anything when i clicked C and the animation didnt play

local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

local tool = script.Parent

local animation = script:WaitForChild("Crounch")
local crouch = humanoid:LoadAnimation(animation)
crouch.Priority = Enum.AnimationPriority.Action
crouch.Looped = true

local equipped = false
local toggle = false

tool.Equipped:Connect(function()
	equipped = true
end)

tool.Unequipped:Connect(function()
	equipped = false
	crouch:Stop()
end)

userInput.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	
	if equipped then
		if input.KeyCode == Enum.KeyCode.C then
			toggle = not toggle
			if toggle then
				crouch:Play()
			elseif not toggle then
				crouch:Stop()
			end
		end
	end
end)

imma save yours when the previous script gets buggy

damn bro that worked.

Summary

This text will be hidden