How to stop the walk animation when the player is crouching

Hi, I would like to know how I can make the walk animation stop when the player is crouching.

Heres the code:

--//Crouch

local CrouchFile = script:WaitForChild("Crouch")
local CrouchTrack
if not CrouchTrack then
	CrouchTrack = Animator:LoadAnimation(CrouchFile)
end

local function Crouching()
	IsCrouching = true
	script.Parent.Values.Crouching.Value = true
	task.spawn(function()
		while task.wait() do
			if not IsCrouching then
				break
			end
			if humanoidRootPart.Velocity.Magnitude < 1 and IsCrouching then
				CrouchTrack:AdjustSpeed(0)
			else
				CrouchTrack:AdjustSpeed(1)
			end
		end
	end)

	CrouchTrack:Play(.3)

	humanoid.JumpHeight = 0
	humanoid.HipHeight = -1
end

local function StopCrouching()
	IsCrouching = false
	script.Parent.Values.Crouching.Value = false
	if CrouchTrack then
		CrouchTrack:Stop()
	end
	humanoid.JumpHeight = 7.2
	humanoid.HipHeight = 0
end


userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end 

	if input.KeyCode == Enum.KeyCode.C and CanCrouch then
		Crouching()
	end
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent then return end 

	if input.KeyCode == Enum.KeyCode.C and CanCrouch then
		StopCrouching()
	end
end)

Video:

The crouch animation priority is Action, so it is higher than the walk animation.

You could get the humanoid’s playing tracks :GetPlayingAnimationTracks() and run through it with a for statement, then if the name is Walking (or whatever it is) then run :Stop() on it

2 Likes

It works!, but know how i can get the animation back when the player stops crouching?

Tysm!, it worked.

Here’s what i do:

			for _, v in ipairs(humanoid:GetPlayingAnimationTracks()) do
				if v.Name == "WalkAnim" then
					v:Stop()
				end
			end

you would just have to find whatever id is used for walking and put an animation instance inside the script with the id set so you could call it (or do it in the script like here):

local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://WalkingIdHere"
humanoid:LoadAnimation(animation):Play() 
1 Like

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