Player won't stay crouched when holding key down

This is probably really simple, but I am using a script that runs a crouch animation when the key C is pressed. This is a free script I found in the Toolbox.

the animation runs when I press C, but the player does not stay down and will instantly come back up to the normal animations.

I am not well versed in Lua coding so I don’t know how to fix this.

local Player = game.Players.LocalPlayer
local Humanoid = Player.Character:WaitForChild('Humanoid')

local RunAnimation = Instance.new('Animation')
local humanim = Player.Character.Humanoid:LoadAnimation(script.CrouchAnimation)
local humanimid = Player.Character.Humanoid:LoadAnimation(script.CrIdleAnimation)
Running = false

local WalkingSpeed = 10
local CrouchSpeed = 8

function Handler(BindName, InputState)
	if InputState == Enum.UserInputState.Begin and BindName == 'RunBind' then
		Running = true
		Humanoid.WalkSpeed = CrouchSpeed
		humanimid:Play()
	elseif InputState == Enum.UserInputState.End and BindName == 'RunBind' then		
		humanim:Stop()
		humanimid:Stop()
		Running = false
		Humanoid.WalkSpeed = WalkingSpeed
	end
end

Humanoid.Running:connect(function(Speed)	
	if Speed >= CrouchSpeed - 1 and Running and not humanim.IsPlaying then
				humanim:Play()
		Humanoid.WalkSpeed = CrouchSpeed
			end
	
	if Speed >= CrouchSpeed + 1 and not Running and humanim.IsPlaying then
		
			humanim:Stop()
		Humanoid.WalkSpeed = WalkingSpeed
			humanimid:Stop()

		
	elseif Speed < CrouchSpeed - 1 and humanim.IsPlaying then
		humanim:Stop()
		
	end
end)

Humanoid.Changed:connect(function()
	if Humanoid.Jump and humanim.IsPlaying then
		humanim:Stop()
		humanimid:Stop()
	end
end)
game:GetService('ContextActionService'):BindAction('RunBind', Handler, true, Enum.KeyCode.C)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

In that script the animation it’s playing when the input begins and stopping when the input ends.
You can make a boolvalue to know if you are already crouching or not, and then playing or stopping the animation.

1 Like

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