How to disable "Shift to Run" when player is Crouching

How to make it that when the player is crouching the player cannot use the Shift key to sprint while crouching.

Crouch Script:

wait(1)
local TweenService = game:GetService("TweenService") --gets service for tween
local tweenPart = game.Players.LocalPlayer.Character.Humanoid --the part you want to tween (you can change this)

local info = TweenInfo.new(
	0.325,           --seconds it takes to complete loop
	Enum.EasingStyle.Back,     --easingstyle (how it moves)
	Enum.EasingDirection.Out,    --easingdirection (which direction)
	0,                    --times repeated (negative number if u want infinite)
	false,                 --reverse (does it go back to its spot)
	0                      --delay time (stoptime before doing the tween)
)

local Goals = {             --YOU CAN CHANGE THESE AND DELETE THE THINGS YOU DON'T WANT TO TWEEN
	CameraOffset = Vector3.new(0,0,0), --where the part will be after tween
}
local info2 = TweenInfo.new(
	0.1,           --seconds it takes to complete loop
	Enum.EasingStyle.Back,     --easingstyle (how it moves)
	Enum.EasingDirection.Out,    --easingdirection (which direction)
	0,                    --times repeated (negative number if u want infinite)
	false,                 --reverse (does it go back to its spot)
	0                      --delay time (stoptime before doing the tween)
)

local Goals2 = {             --YOU CAN CHANGE THESE AND DELETE THE THINGS YOU DON'T WANT TO TWEEN
	CameraOffset = Vector3.new(0,0,0), --where the part will be after tween
}

local PartTween = TweenService:Create(tweenPart, info, Goals) --gets all the info and goals and creates tween
local PartTween2 = TweenService:Create(tweenPart, info2, Goals2) --gets all the info and goals and creates tween

local animidle = tweenPart:LoadAnimation(script.Parent.Idle)

local animwalk = tweenPart:LoadAnimation(script.Parent.Walk)
local crouching = false
local waiting = false

local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		if waiting == false then
			if crouching == false then
				tweenPart.WalkSpeed = 8
				waiting = true
				crouching = true
				animidle:Play()
				PartTween:Play() --plays it
				tweenPart.Running:Connect(function(Speed)
					if crouching == true then
						if Speed >= 1 then
							animidle:Stop()
							if animwalk.IsPlaying == false then
								animwalk:Play()
							end

						else
							animidle:Play()
							animwalk:Stop()
						end
					end
				end)
				wait(0.05)
				waiting = false
			else
				tweenPart.WalkSpeed = 16
				waiting = true
				crouching = false
				animidle:Stop()
				animwalk:Stop()
				PartTween2:Play()
				wait(0.05)
				waiting = false
			end
		end

	end
	if input.KeyCode == Enum.KeyCode.RightShift then
		if waiting == false then
			if crouching == false then
				tweenPart.WalkSpeed = 8
				waiting = true
				crouching = true
				animidle:Play()
				PartTween:Play() --plays it
				tweenPart.Running:Connect(function(Speed)
					if crouching == true then
						if Speed >= 1 then
							animidle:Stop()
							if animwalk.IsPlaying == false then
								animwalk:Play()
							end

						else
							animidle:Play()
							animwalk:Stop()
						end
					end
				end)
				wait(0.05)
				waiting = false
			else
				tweenPart.WalkSpeed = 16
				waiting = true
				crouching = false
				animidle:Stop()
				animwalk:Stop()
				PartTween2:Play()
				wait(0.05)
				waiting = false
			end
		end

	end
end)
script.Parent.MouseButton1Click:Connect(function()
	if waiting == false then
		if crouching == false then
			tweenPart.WalkSpeed = 8
			waiting = true
			crouching = true
			animidle:Play()
			PartTween:Play() --plays it
			tweenPart.Running:Connect(function(Speed)
				if crouching == true then
					if Speed >= 1 then
						animidle:Stop()
						if animwalk.IsPlaying == false then
							animwalk:Play()
						end

					else
						animidle:Play()
						animwalk:Stop()
					end
				end
			end)
			wait(0.05)
			waiting = false
		else
			tweenPart.WalkSpeed = 16
			waiting = true
			crouching = false
			animidle:Stop()
			animwalk:Stop()
			PartTween2:Play()
			wait(0.05)
			waiting = false
		end
	end
end)```
2 Likes

I would very much appreciate it if someone could help, thanks.

Could be something as simple as adding a Crouching debounce…

if crouching then return end

I don’t know where you’d add it in your code though but, it looks like you already have some sort of crouching variable…

@Project_Aurora I do have a crouch function, yes however I need it that when the player would to crouch they can’t sprint and the speed will remain as 16, thank you for your help.

use boolvalues to check if the player is crouching then dont let them sprint if otherwise let them

I don’t know how to script that.

You most likely already have a function that runs whenever a player joins the game. Then in the code that runs, all you have to do is create a new instance of a BoolValue and bind it to the player. Name it to ‘isCrouching’ or some of the sort, and set it’s value to false. This value will now determine whether the player crouches or not.

Make sure that in your sprinting function, you first have to check for if isCrouching == false then to not run the function if the player’s crouching value is true.

In the crouching function, change the value of the ‘isCrouching’ boolean by using isCrouching.Value = true. When the player lets go of the crouch button, change the value back to false.

I also have a run script, would I need to show that script aswell?

In this case you should make the code of ShiftToRun localscript inside the same Crouch localscript for them to share bool values easily, but you could also make a BoolValue Instance like the guy above me suggested which will replicate between these two LocalScripts since they are LocalScripts and are in your Character or Player. If you put the code in the same localscript, in the Running code you make a “isRunning” boolean and set it to true when inputbegan and false when inputended and check if “crounching” is false so it will not run if it is true. And in the Crouching UIS functions you also check if “isRunning” is false, so it will not crouch when the player is running.

The Crouch script is in StaterGui so when the player is on Mobile a ImageButton will show (only for Mobile)

No, I simply said to add a condition to check for the BoolValue.

You probably have the line;

UIS.InputBegan:Connect(function(input, GPE)
	if input.KeyCode == Enum.KeyCode.LeftShift then
      ...

Add one line like so;

UIS.InputBegan:Connect(function(input, GPE)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		if not isCrouching then --//If crouching is false, the function will execute
          ...
1 Like

I don’t want to make the code for you but I can atleast explain how you will do this:

  1. Create a folder inside the player (when they join using .PlayerAdded)
  2. Insert a boolvalue called “Iscrouching” and set the value to false inside the folder.
  3. Go back to your crouching script and when the player is crouching enable that value (note if you are using a localscript then you’d have to use a serverscript and a remotevent)
  4. Now go to your sprint script and check if Iscrouching == true then don’t let the player sprint.
1 Like

not the best idea, this will be slightly slow, you should use variables.

Not really it’s almost the same as variables, but uses objects, mine is just a solution without having to combine both sprint and crouch scripts

still slower by a .02 ms :3

How did you even calculate that?

by benchmarking it you scallywag, now empty the compartments of your pantaloons!

It’s still a solution. .02 ms is actually not that big of a difference so don’t care. Judging by your behavior you definitely have gotten :nerd:'ed from other people.

2 Likes

I don’t have the time to fully help you but i can give you a good tip: Use “task.wait()” instead of wait() because the task. library has functions like task.spawn() task.wait() and task.delay() these functions are better because they work on every frame. Therefore the task. library is more Efficient

@Project_Aurora @tins4packabs @LinusKat @Iemny @f8003199martins @MikeartsRBLX

Thank you all for your help, but I can’t script very well, I’m mainly a Developer on my side.

1 Like