Why won't this Crouch script work?

Idk why this won’t work:



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


local animationIdle = humanoid:LoadAnimation(script:WaitForChild("CrouchIdle"))
local animationMoving = humanoid:LoadAnimation(script:WaitForChild("CrouchMoving"))


local crouching = false


uis.InputBegan:Connect(function(inp, processed)
	
	
	if processed  then return end
	
	
	if inp.KeyCode == Enum.KeyCode.C then
		
		crouching = not crouching
	end
end)


game:GetService("RunService").Heartbeat:Connect(function()
	
	script.Parent.HumanoidRootPart.CanCollide = false
	
	
	if crouching then
		
		
		humanoid.WalkSpeed = 1
		
		animationIdle:Play()
		
		
		if humanoid.MoveDirection.Magnitude > 0 then
			
			if not animationMoving.IsPlaying then animationMoving:Play() end
			
			
		else
			
			animationMoving:Stop()
		end
		
		
	else
		
		animationIdle:Stop()
		animationMoving:Stop()
		
		humanoid.WalkSpeed = 16
	end
end)

Also the script is in StarterCharacterScripts.

1 Like

The script is still gonna be playing the Idle animation when the player is moving so move the animationIdle:Play() line of code to where you stop the moving animation when the player is not moving
and then to stop the idle animation playing when the player starts moving put animationIdle:Stop() line of code in where you start the moving animation when the player starts moving

Change these lines

animationIdle:Play()
		
		
if humanoid.MoveDirection.Magnitude > 0 then
	
	if not animationMoving.IsPlaying then animationMoving:Play() end
			
			
else
			
	animationMoving:Stop()
end

To these

if humanoid.MoveDirection.Magnitude > 0 then
	
	if not animationMoving.IsPlaying then animationMoving:Play() end
	
	animationIdle:Stop()
else
	
	animationMoving:Stop()
	animationIdle:Play()
end
1 Like

It’s still not working. Is there something wrong with my avatar?

What exactly isn’t working?
The animations?

you might need to change the animation priority of the moving animation to be higher than the idle

Yea, the animations won’t play, the onpy thing that happens is the palyer walking slower. No animation

How do I do cahnge the priority

In the roblox animation editor, you can change priority here:

change the animation priority and then upload the animation.
Animation priority goes:
Core < Idle < Movement < Action < Action2 < Action3 < Action4

where Core has the least priority and Action4 has the most priority, overwriting all other animations.

1 Like

What should i set the priority to?

Use this:

local Deb = false

local function CheckAlive()
	local Hum = script.Parent:FindFirstChild("Humanoid")
	local Head = script.Parent:FindFirstChild("Head")
	local HumanRoot = script.Parent:FindFirstChild("HumanoidRootPart")
	if Hum and Hum.Health > 0 and Head and HumanRoot then
		return true
	else
		return false
	end
end

local function CheckHead()
	if not CheckAlive() then return end

	local rayOrigin = script.Parent.Head.Position
	local rayDirection = Vector3.new(0, 2, 0)

	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)

	if raycastResult then
		local hitPart = raycastResult.Instance
		if hitPart.Transparency == 0 then
			return false
		else
			return true
		end
	else
		return true
	end
end


function Crouch(actionName, inputState, inputObject)
	if not CheckAlive() then return end
	if inputState == Enum.UserInputState.Begin then
		if not Deb then
			Deb = true
			game:GetService("TweenService"):Create(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 0.3}):Play()
		else
			if CheckHead() then
				Deb = false
				game:GetService("TweenService"):Create(script.Parent:FindFirstChild("Humanoid"), TweenInfo.new(0.2), {HipHeight = 2}):Play()
			end
		end
	end
end

game:GetService("ContextActionService"):BindAction("Crouch", Crouch, true, Enum.KeyCode.LeftControl, Enum.KeyCode.C, Enum.KeyCode.ButtonB)

Then just add the animations. If you need help with that let me know and I can help.

  1. You’re missing a input ended event so the script never knows when you stopped holding c

  2. You made it so when they press c crouching is false, this means that crouching will always be false thus your crouch animations will never play

Additionally you don’t have to do crouching = not crouching, you can do crouching = false and it will work just fine