Crouch animation stops when i start to walk, or crouching (while moving) stops when I stop walking

Hello developers! In a game I’m working on, there is a crouch animation. This is the code:

local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://7120081479"
Animation.Parent = player.Character

local Animate = true
local debounce = false

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftControl then
        if not debounce then
            debounce = true
            Animate = Humanoid:LoadAnimation(Animation)
			            Animate:Play()
			Humanoid.WalkSpeed = Humanoid.WalkSpeed - 9
			Humanoid.JumpPower = 0
        else
			            Animate:Stop()
			Humanoid.WalkSpeed = 16
			Humanoid.JumpPower = 50

            debounce = false
       end  

	end
end) 

My issue is this:


Whenever I stop moving in the crouch position, the animation stops, en when I start moving when in the crouch position, the same happens.
It’s a problem with the animation I think, because the speed says as it should when the animation glitches.
(The animation is a test, it’s a single keyframe on the torso, which repeats itself)

I hope you can help!

1 Like

I will set the walkspeed to 0 and change the walk animation id in Player Animate Script then set the walkspeed to the speed you want

Can you explain further? I’m not good at coding (I made this by combining code from free models lol)

try making the parent part of the local script maybe?

also i wouldn’t recommend making a variable called “Animate = true”

Can you explain further? I’m not good at coding (I made this by combining code from free models lol)

Animation.Parent = script

you can also just make an animation object inside of the script.

i would delete “local Animate = true” if i were you

also im on mobile, so i basically cant try it out at the moment…

It still works with this, but it did not solve the animation issue :grimacing:

how about removing the debounce?

Like removing those lines completely?

yes. try removing the lines with debounce in them

the debounce might be your issue

1 Like
local player = game.Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")

local crouchId = "rbxassetid://7120081479"
local walkId = "rbxassetid://913402848"

local Animate = true
local debounce = false

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.LeftControl then
        if not debounce then
            debounce = true
            local lastWalkspeed = Humanoid.WalkSpeed
            Humanoid.WalkSpeed = 0
            Character.Animate.walk.WalkAnim.AnimationId = crouchId
			Humanoid.WalkSpeed = lastWalkspeed - 9
			Humanoid.JumpPower = 0
        else
            Humanoid.WalkSpeed = 0
            Character.Animate.walk.WalkAnim.AnimationId = walkId
			Humanoid.WalkSpeed = 16
			Humanoid.JumpPower = 50

            debounce = false
       end  

	end
end) 

if it doesnt work try changing walk.WalkAnim to run.RunAnim
this is what i use for my running/crouching system

1 Like

I will try both of these out later. Thanks for the help!

no problem :smile:

(char character limit)

1 Like

i might try this out as well :wink:

1 Like

Pretty sure the debounce is the issue since you’re using an if and else statement. Basically when the debounce runs the first time it won’t ever be turned back to false since the “debounce = false” is in the else section which was basically voided meaning that the next time you would click LeftControl it would skip the “if not debounce then” section and go straight to else which would break the animate and it just continues in a loop.
The way to fix this would probably be something like this

UIS.InputBegan:Connect(function(Input)
    if not debounce then
        debounce = true

       if Input.KeyCode == Enum.KeyCode.LeftControl then
Animate = Humanoid:LoadAnimation(Animation)
			            Animate:Play()
			Humanoid.WalkSpeed = Humanoid.WalkSpeed - 9
			Humanoid.JumpPower = 0

     else  


  Animate:Stop()
			Humanoid.WalkSpeed = 16
			Humanoid.JumpPower = 50
          end  
            debounce = false
      end
end)

So… That wasn’t the case. Removing the debounce from the script will result in it not functioning at all.

This works but the only problem is that I want to be able to crouch when I’m standing still too.

This broke the script entirely. It didn’t do anything anymore :grimacing:

is this in the startercharacterscripts folder?

Yes. It is inside of the startercharacterscripts folder.

i recommend inserting a animation object inside of the local script.

after that, you put the animationID inside of the animation object.

instead of just making one…

What is this crouch animation’s priority? If it’s at the Core priority, the walking and standing animation will overlap with the crouching one. You can just move it up one priority, Idle, republish the animation and use the script in the OP.

All of Roblox’s animations run at Core priority by the way.

1 Like