Need help converting User Input Service Crouch script to Context Action Service

I want to make a nice R6 crouch script, and it’s exactly what I made! but, in combination with my sprint script, and my emote script, it breaks. You can sprint while crouching and get 24 speed instead of 8 speed. And when typing, pressing C enables the crouching animation. I have tried to convert it to CAS(context action service), but, I can’t figure out how to make it hold C to activate and let go of C to disable. Here is the script. It is in StarterCharacterScripts.

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.C then
		char.Humanoid.WalkSpeed = 8
		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://6616895835"
		PlayAnim = char.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		char.Humanoid.Running:connect(function(Speed)
			if Speed <= 0.0001 then
				PlayAnim:AdjustSpeed(0.001)
			else
				PlayAnim:AdjustSpeed(1)
			end
		end)	
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.C then
		char.Humanoid.WalkSpeed = 16
		PlayAnim:Stop()
	end
end)
3 Likes
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.C and UIS:GetFocusedTextBox() == nil then
		-- just disable the sprint script
		char.Humanoid.WalkSpeed = 8
		local Anim = Instance.new("Animation")
		Anim.AnimationId = "rbxassetid://6616895835"
		PlayAnim = char.Humanoid:LoadAnimation(Anim)
		PlayAnim:Play()
		char.Humanoid.Running:connect(function(Speed)
			if Speed <= 0.0001 then
				PlayAnim:AdjustSpeed(0.001)
			else
				PlayAnim:AdjustSpeed(1)
			end
		end)	
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.C then
		-- re-enable it 
		char.Humanoid.WalkSpeed = 16
		PlayAnim:Stop()
	end
end)