Contextaction service Crouching isn't working

Hi I’m trying to make a movement system with contextaction service but the crouching system isn’t working and it doesn’t show any errors in the output and in the script editor, I tried looking for misspelling but there’s none and I’m confuse what I did wrong.
Here is the video of it:

And here’s the script of it:

local userinputService = game:GetService("UserInputService")
local contextactionService = game:GetService("ContextActionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
local animator = human:FindFirstChildOfClass("Animator")
local isRunning = false
local HumanoidRootPart:BasePart = char:WaitForChild("HumanoidRootPart")
local Camera = game.Workspace.Camera
local Storage = ReplicatedStorage.Storage

local anim = Instance.new('Animation', animator)
anim.AnimationId = 'rbxassetid://14185315909'
local playAnim = animator:LoadAnimation(anim)


--Crouching--
local SoundsFolder = Storage.Sounds
local CrouchSounds = SoundsFolder.Crouch
local GetUpSound = SoundsFolder.GetUp
local AnimationFolder = Storage.Animations
local CrouchAnimation = human:LoadAnimation(AnimationFolder.Crouch)
local CrouchWalking = human:LoadAnimation(AnimationFolder.CrouchWalk)
local Crouching = false
local Crouchwalk = false


local function movementKeys()
	return userinputService:IsKeyDown(Enum.KeyCode.W) or userinputService:IsKeyDown(Enum.KeyCode.A) or userinputService:IsKeyDown(Enum.KeyCode.S) or userinputService:IsKeyDown(Enum.KeyCode.D)
end

local function processAction(action, state, input)
	if action == "Sprint" then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if state == Enum.UserInputState.Begin then
				if movementKeys() then
					char.Humanoid.WalkSpeed = 24
					playAnim:Play()
					isRunning = true
				elseif not movementKeys() then
					char.Humanoid.WalkSpeed = 16
					playAnim:Stop()
					isRunning = false
				end
			elseif state == Enum.UserInputState.End then 
				char.Humanoid.WalkSpeed = 16
				playAnim:Stop()
				isRunning = false
			end
		end
	end
end



local function CrouchStarted(input,action,state)
	if action == "Crouch" then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if state == Enum.UserInputState.Begin then
				CrouchAnimation:Play()
				char.Humanoid.Walkspeed = 6
				Camera.CameraSubject = char.Head;
				Camera.FieldOfView = 65
				Crouching = true
				isRunning = false
				human.Running:Connect(function(Speed)
					if Speed >0 and Crouching == true then
						if CrouchAnimation.IsPlaying == true then
							CrouchAnimation:Stop()
							task.wait(0.1)
							if true then
								CrouchWalking:Play()
								Crouchwalk = true
								if Crouchwalk == true then
									Crouching = false
								end
							end
						end
						
					end
				end)
				human.Running:Connect(function(Speed)
					if Speed == 0 and Crouchwalk == true and Crouching == false then
						if CrouchWalking.IsPlaying == true then
							Crouchwalk = false
							Crouching = true
							CrouchWalking:Stop()
							CrouchAnimation:Play()
						end
					end
				  end)
				end
			
		elseif state == Enum.UserInputState.End then
			CrouchAnimation:Stop()
			char.Humanoid.Walkspeed = 16
			Camera.FieldOfView = 70
			Camera.CameraSubject = char.human
			human.CameraMinZoomDistance = 0.5
			Crouching = false
			isRunning = true
			GetUpSound:Play()
	        end
          end
       end


local function Movement()		
	if isRunning == true then 
		if human.MoveDirection ~= Vector3.new(0, 0, 0) then --Still moving

		else
			char.Humanoid.WalkSpeed = 16
			playAnim:Stop()
			isRunning = false
		end
		game:GetService("RunService").Heartbeat:Connect(function(deltatime)		
			if (HumanoidRootPart.CFrame.LookVector:Dot(human.MoveDirection)) <= -0.75 then
				char.Humanoid.WalkSpeed = 16
				playAnim:Stop()
				isRunning = false
		    end
	    end)
	end
end

contextactionService:BindAction("Sprint", processAction, true, Enum.KeyCode.LeftShift)
contextactionService:SetPosition("Sprint", UDim2.new(0.72, -65, 0.20, -8))
contextactionService:BindAction("Crouch", CrouchStarted, true, Enum.KeyCode.C)
human:GetPropertyChangedSignal("MoveDirection"):Connect(Movement)

Please let me know if I did something wrong in my code and tell me how to fix it.

edit: nvm i fixed by myself

local function CrouchStarted(input,action,state)
	if state == Enum.UserInputState.Begin then
		CrouchAnimation:Play()
		char.Humanoid.Walkspeed = 6
		Camera.CameraSubject = char.Head;
		Camera.FieldOfView = 65
		Crouching = true
		isRunning = false
		human.Running:Connect(function(Speed)
			if Speed >0 and Crouching == true then
				if CrouchAnimation.IsPlaying == true then
					CrouchAnimation:Stop()
					task.wait(0.1)
					if true then
						CrouchWalking:Play()
						Crouchwalk = true
						if Crouchwalk == true then
							Crouching = false
						end
					end
				end

			end
		end)
		human.Running:Connect(function(Speed)
			if Speed == 0 and Crouchwalk == true and Crouching == false then
				if CrouchWalking.IsPlaying == true then
					Crouchwalk = false
					Crouching = true
					CrouchWalking:Stop()
					CrouchAnimation:Play()
				end
			end
		end)
		
	elseif state == Enum.UserInputState.End then
		CrouchAnimation:Stop()
		char.Humanoid.Walkspeed = 16
		Camera.FieldOfView = 70
		Camera.CameraSubject = char.human
		human.CameraMinZoomDistance = 0.5
		Crouching = false
		isRunning = true
		GetUpSound:Play()
	end
end
1 Like

Hi, sorry for bumping but I’ve encountered the same issue. Can you explain how you fixed it?

Using RenderStepped so it sprint instantly base on your framerate
Edit: I can’t show you the scripts because i already deleted my rbx files long time ago, sorry