Can't make player unable to sprint while crouched

  1. What do you want to achieve?
    I want to make the player unable to run while crouched

  2. What is the issue? Include screenshots / videos if possible!
    I managed to make the player unable to crouch while running, which does work

It also works while player is already seated in a seat he cannot crouch, but other problem is that he can run while sitting and I would like that not to happen.

And the main problem what I’m trying to solve is to make what while player is crouching he can’t run, but i dont know how to do that in the script, i already try with values using if IsCrouching == true and Sprinting == true then but nothing works

robloxapp-20221221-2011110.wmv (1,7 MB)

I would like someone help me to me realise how to solve my problems :pleading_face::pray:

This is my entire script, sorry if is to long, but includes stamina bar & sprinting and crouching system located in StarterGui

--Variables Sprint & Stamina System

local camera = game.Workspace.CurrentCamera

local TweeningService = game:GetService("TweenService")

local UIS = game:GetService('UserInputService')

local Bar = script.Parent:WaitForChild('Stamina'):WaitForChild('Bar')

local NormalWalkSpeed = 12
local NewWalkSpeed = 22
local stamina = 10
local sprinting = false

local player = game.Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer.Character
local character = player.Character

local ExhaustedG = script.Parent.Parent:WaitForChild("ExhaustedGUI"):WaitForChild("Frame")

-- Variables Crouch System

local char = player.Character or player.CharacterAdded:Wait()
local Humanoid = char:WaitForChild("Humanoid")

local CrouchAnim = script:WaitForChild("Crouch Anim")
local CAnim = Humanoid:LoadAnimation(CrouchAnim)

local CrouchSpeed = 6

local C = Enum.KeyCode.C

local IsCrouched = false
local IsSeated = false

-- Tween Functions



        local FOVChanges = {
	        FieldOfView = 90
        }
        local TweenInformation = TweenInfo.new(
	        1, --tween length
	        Enum.EasingStyle.Quint, --easing Style
	        Enum.EasingDirection.Out, --easing Direction
	        0, --repitition time
	        false,
	        0 --delay				
        )

        local tween = TweeningService:Create(camera,TweenInformation,FOVChanges)



        local FOVChanges2 = {
	        FieldOfView = 70
        }
        local TweenInformation2 = TweenInfo.new(
	        1, --tween length
	        Enum.EasingStyle.Quint, --easing Style
	        Enum.EasingDirection.Out, --easing Direction
	        0, --repitition time
	        false,
	        0 --delay				
		)
		
        local tween2 = TweeningService:Create(camera,TweenInformation2,FOVChanges2)

		local GUIFadeIn = {
		ImageTransparency = 0
		}
		
		local GUIFadeOut = {
		ImageTransparency = 1
		}
		
		local ExhaustedGFadeIn = TweeningService:Create(ExhaustedG,TweenInformation, GUIFadeIn)
		local ExhaustedGFadeOut = TweeningService:Create(ExhaustedG,TweenInformation2, GUIFadeOut)

-- Functions

UIS.InputBegan:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftControl and gameProcessed == false then
		character.Humanoid.WalkSpeed = NewWalkSpeed
		sprinting = true
		while stamina > 0 and sprinting do
			stamina = stamina - .0925
			Bar.Size = UDim2.new(stamina / 10, 0, 1, 0)
			tween:Play()
			wait()
			if stamina <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
				script.Parent.Parent.Exausted:Play()
				ExhaustedGFadeIn:Play()
			end
		end
	end
end)

UIS.InputEnded:connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftControl and gameProcessed == false then
		character.Humanoid.WalkSpeed = NormalWalkSpeed
		sprinting = false
		while stamina < 10 and not sprinting do
			stamina = stamina + .0125
			Bar.Size = UDim2.new(stamina / 10, 0, 1, 0)
			tween2:Play()
			ExhaustedGFadeOut:Play()
			wait()
			if stamina <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end
end)

local State = 1

script.Parent.Parent:WaitForChild("SprintButton").ImageButton.MouseButton1Click:Connect(function()
	character.Humanoid.WalkSpeed = NewWalkSpeed
	sprinting = true
	if State == 1 then
		while stamina > 0 and sprinting do
			State = 0
			stamina = stamina - .09
			Bar.Size = UDim2.new(stamina / 10, 0, 1, 0)
			tween:Play()
			wait()
			if stamina <= 0 then
				character.Humanoid.WalkSpeed = NormalWalkSpeed
				script.Parent.Parent.Exausted:Play()
				ExhaustedGFadeIn:Play()
			end
		end
	else
		if State == 0 then
			character.Humanoid.WalkSpeed = NormalWalkSpeed
			sprinting = false
			while stamina < 10 and not sprinting do
				State = 1
				stamina = stamina + .01
				Bar.Size = UDim2.new(stamina / 10, 0, 1, 0)
				tween2:Play()
				ExhaustedGFadeOut:Play()
				wait()
				if stamina <= 0 then
					character.Humanoid.WalkSpeed = NormalWalkSpeed
				end
			end
		end
	end
end)

Humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
	if Humanoid.Sit == true then
		IsSeated = true
	else
		if Humanoid.Sit == false then
			IsSeated = false
		end
	end
end)

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.C and sprinting == false then
		if IsCrouched == false and IsSeated == false then
			IsCrouched = true
			Humanoid.WalkSpeed = CrouchSpeed
			Humanoid.JumpPower = 0
			Humanoid.JumpHeight = 0
			CAnim:Play()
			if IsCrouched == false and IsSeated == true and sprinting == true then
				print("You can't crouch while you are on a seat")
			end
		else
			if IsCrouched == true and IsSeated == false then
				IsCrouched = false
				Humanoid.WalkSpeed = NormalWalkSpeed
				Humanoid.JumpPower = 50
				Humanoid.JumpHeight = 7.2
				CAnim:Stop()
			else
				if IsCrouched == true and IsSeated == true then
					IsCrouched = false
					Humanoid.WalkSpeed = NormalWalkSpeed
					Humanoid.JumpPower = 50
					Humanoid.JumpHeight = 7.2
					CAnim:Stop()
				else
					if IsCrouched == true and sprinting == true then
						IsCrouched = false
						Humanoid.WalkSpeed = NormalWalkSpeed
						Humanoid.JumpPower = 50
						Humanoid.JumpHeight = 7.2
						CAnim:Stop()
					end
				end
			end
		end
	else
		if key.KeyCode == Enum.KeyCode.C and sprinting == true then
			print("You can't run while crouching")
		end
	end
end)
1 Like

I figured out how to fix it by myself

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