Crouch animation plays when holding CTRL and standing still

  1. What do you want to achieve? I want the animation to play only when the player is
    holding CTRL and Moving

  2. What is the issue? Whenever ur standing still and u hold ctrl the crouch walk animation plays


local NormalWalkSpeed = 16
local CrouchSpeed = 10
local AnimID = "rbxassetid://13281476191"

local cas = game:GetService("ContextActionService")
local Leftc = Enum.KeyCode.LeftControl
local RightC = Enum.KeyCode.RightControl
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")
local CrouchAnim = Instance.new("Animation")
CrouchAnim.AnimationId = AnimID
local CAnim = Humanoid:LoadAnimation(CrouchAnim)

local Camera = game.Workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(key, gameProcessed)
	if gameProcessed then return end
	if key.KeyCode == Enum.KeyCode.LeftControl then
		Humanoid.WalkSpeed = CrouchSpeed
		Humanoid.JumpPower = 0
		Humanoid.JumpHeight = 0
		CAnim:Play()
		CAnim.Looped = true
		player.PlayerGui.StaminaAndHealth.SprintHandler.Enabled = false
	
		
	end
end)

UIS.InputEnded:Connect(function(key, gameProcessed)
	if gameProcessed then return end
	if key.KeyCode == Enum.KeyCode.LeftControl then
		Humanoid.WalkSpeed = NormalWalkSpeed
		Humanoid.JumpPower = 50
		Humanoid.JumpHeight = 7.2
		CAnim:Stop()
		CAnim.Looped = false
		wait(1)
		player.PlayerGui.StaminaAndHealth.SprintHandler.Enabled = true

	end
end)

-------- Mobile Buttons

local function handleContext(name, state, input)
	if state == Enum.UserInputState.Begin then
		Humanoid.WalkSpeed = CrouchSpeed
		CAnim:Play()
		player.PlayerGui.StaminaAndHealth.SprintHandler.Enabled = false
		CAnim.Looped = true
	else
		Humanoid.WalkSpeed = NormalWalkSpeed
		CAnim:Stop()
		CAnim.Looped = false
		wait(1)
		player.PlayerGui.StaminaAndHealth.SprintHandler.Enabled = true

	end
end

cas:BindAction("Crouch", handleContext, true, Leftc, RightC)
cas:SetPosition("Crouch", UDim2.new(.2, 0, .5, 0))
cas:SetTitle("Crouch", "Crouch")
cas:GetButton("Crouch").Size = UDim2.new(.3, 0, .3, 0)

I would assume you would need to put something about HumanoidStateType in there to ensure the human is running. I have never used this but I did some pretty basic research. The documentation I am using can be found
here: HumanoidStateType | Roblox Creator Documentation

1 Like

Thank you man i actually didnt know this exists lol

Also just to add on. You don’t use RightC or Leftc variables in the code.

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