Crouching animation does not work

So I recently tried to code a function for crouching in a game. For some reason, the script does not play when i press “C”. Anyone knows why this is happening?

local UserInputService = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = game.WaitForChild("Humanoid")
local isRunning = false

local Animation = Humanoid:LoadAnimation(script:WaitForChild("Animation"))


UserInputService.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.C then
		if not isRunning then
			isRunning = true
			Animation:Play()
			Humanoid.WalkSpeed = 10
		else
			Animation:Stop()
			Humanoid.WalkSpeed = 16
			isRunning = false
		end
	end
end)



Humanoid variable is wrong. You should be referencing the Character variable you made above, not the game.

local Humanoid = Character:WaitForChild(“Humanoid”)

Hi!

I changed it, but for some reason it still does not work. Im in R15. Do you think that it could be the animation?

Did you make the game in R6 or R15? If the animation is in R6 and the game is in R15 then the animation won’t work.

1 Like

Do you own the animation OR is the game owned by a group? (And is the Priority set to ACTION) Also Humanoid:LoadAnimation() is depracated use Humanoid:WaitForChild("Animator"):LoadAnimation()

1 Like

Yes if you are in r15 and your animation is r6, or vice versa, then you need to change it so that both the game and animation are r15 or r6.

Additionally, make sure the animation is looped, and that the priority is set to any of the ones that start with “Action” (Action 1, Action 2, etc.).

2 Likes
local UserInput = game:GetService("UserInputService")

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

local IsCrouching = false

local Animation = script:WaitForChild("Animation")
local Track = Humanoid:LoadAnimation(Animation)

UserInput.InputBegan:Connect(function(Key, Processed)
	if Processed then return end
	
	if Key.KeyCode == Enum.KeyCode.C then
		IsCrouching = not IsCrouching
		if IsCrouching then
			Humanoid.WalkSpeed = 10
			Track:Play()
		else
			Humanoid.WalkSpeed = 16
			Track:Stop()
		end
	end
end)

I tested this with one of Roblox’s animations (Cartoony Run) and it worked.