How do I prevent sprinting while crouching

I’m currently working on a game and I have encountered an issue.
Whenever I crouch, I can still run. Thus making the crouch feature practically useless.
Does anybody know how I could possibly fix it?

Crouch script:

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 = 16
local CrouchSpeed = 10

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)

Run script:


local UIS = game:GetService("UserInputService")
local RunSpeed = 25
local Player = game.Players.LocalPlayer
local Stamina = script.Parent:WaitForChild("CurrentStamina")
local IsSprinting = script.Parent:WaitForChild("IsSprinting")

local Character = workspace:WaitForChild(Player.Name)
local Humanoid = Character:WaitForChild('Humanoid')

local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = 'rbxassetid://15904280685'
RAnimation = Humanoid:LoadAnimation(RunAnimation)

local SoundFX = script.RunSound

UIS.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		if Stamina.Value > 5 then
			Player.Character.Humanoid.WalkSpeed = RunSpeed
			IsSprinting.Value = true
			RAnimation:Play()
			SoundFX:Play()
		end
	end
end)
UIS.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.LeftShift then
		IsSprinting.Value = false
		Player.Character.Humanoid.WalkSpeed = 16
		RAnimation:Stop()
		SoundFX:Stop()
	end
end)

Thanks for the support in advance.

There’s probably some better way of doing this, but here is how I would do it:

  1. I would create a bool value in each player that joins that is called crouching and is set to false
  2. Each time the player crouches, the crouch bool value turns to true and vice versa
  3. Each time the player tries to run, it checks if the crouch bool value is true. If it is, then nothing happens
1 Like

One way or another, the script for crouching needs to know whether the player is running or not.

You can be flexible on how you want to detect the player running. You could combine both scripts for them to share a variable that can tell whether a player is running, or use an external attribute/value object.

You could also use HumanoidStateType or Humanoid.Magnitude.

In your case, the running script seems to have defined IsSprinting, which is a BoolValue. Your crouch script should simply use that value object and check if its value is true or false.

1 Like