Sprinting issues

I have an issue like when I press w, left shift and s.

footage:

local Player = game:GetService("Players").LocalPlayer
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local UIS = game:GetService("UserInputService")
local Values = script:WaitForChild("Values")
local Camera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")
local SprintFOVInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
local SprintFOVTween = TweenService:Create(Camera, SprintFOVInfo, {["FieldOfView"] = Values.SprintFOV.Value})
local WalkFOVInfo = TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out, 0, false, 0)
local WalkFOVTween = TweenService:Create(Camera, SprintFOVInfo, {["FieldOfView"] = Values.NormalFOV.Value})
local IsRunning = workspace:WaitForChild("SprintFolder").IsRunning
local CanRun = workspace:WaitForChild("SprintFolder").CanRun
local CanRunClientValue = Values:WaitForChild("CanRunClientValue")
local CanRunClient = true
local Jumped = false
local Cooldown = 1

local function KeyPressed(Input)
	local keyispressed = UIS:GetKeysPressed()
	for _, key in ipairs(keyispressed) do
		if key.KeyCode == Input then
			return true
		end
	end
	return false
end


local function StopRun()
	if IsRunning.Value == false then
		if CanRunClient then
			CanRunClient = false
		end
		IsRunning.Value = false
		WalkFOVTween:Play()
		Humanoid.WalkSpeed = Values.Walk.Value
	end
end


local function StartRun()
	if IsRunning.Value == true then
		IsRunning.Value = true
		SprintFOVTween:Play()
		Humanoid.WalkSpeed = Values.Speed.Value
	end
end

UIS.InputBegan:Connect(function(Input, gameProcessedEvent)
	if IsRunning.Value == false and Humanoid.MoveDirection.Magnitude > 0 and CanRun.Value == true and CanRunClient and CanRunClientValue.Value == true and not Jumped then
		if KeyPressed(Enum.KeyCode.W) and KeyPressed(Enum.KeyCode.LeftShift) then
			StartRun()
			IsRunning.Value = true
			SprintFOVTween:Play()
			Humanoid.WalkSpeed = Values.Speed.Value
		else
			if IsRunning.Value == true then
				StopRun()
				if CanRunClient then
					CanRunClient = false
				end
				IsRunning.Value = false
				WalkFOVTween:Play()
				Humanoid.WalkSpeed = Values.Walk.Value
				wait(Cooldown)
				if not CanRunClient then
					CanRunClient = true
				end
			else
				if IsRunning.Value == true then
					StopRun()
					if CanRunClient then
						CanRunClient = false
					end
					IsRunning.Value = false
					WalkFOVTween:Play()
					Humanoid.WalkSpeed = Values.Walk.Value
					wait(Cooldown)
					if not CanRunClient then
						CanRunClient = true
					end
				end
			end
		end
	end
end)

UIS.InputEnded:Connect(function(Input, gameProcessedEvent)
	if IsRunning.Value == true then
		if Input.KeyCode == Enum.KeyCode.W or Input.KeyCode == Enum.KeyCode.LeftShift then
			StopRun()
			if CanRunClient then
				CanRunClient = false
			end
			IsRunning.Value = false
			WalkFOVTween:Play()
			Humanoid.WalkSpeed = Values.Walk.Value
			wait(Cooldown)
			if not CanRunClient then
				CanRunClient = true
			end
		end
	end
end)

local Seated = false

Humanoid.StateChanged:Connect(function(newState)
	if newState == Enum.HumanoidStateType.Jumping then
		if not Jumped then
			Jumped = true
		end
		if CanRunClient and Seated then
			CanRunClient = true
			CanRun.Value = true
		end
	end
	if newState == Enum.HumanoidStateType.Landed then
		if Jumped then
			Jumped = false
		end
		wait(Cooldown)
		if not CanRunClient then
			CanRunClient = true
		end
	end
end)

Humanoid.Died:Connect(function()
	WalkFOVTween:Play()
	script:Destroy()
end)

Humanoid.Seated:Connect(function()
	if Humanoid ~= nil then
		if not Seated then
			Seated = true
		end
		Humanoid:UnequipTools()
		WalkFOVTween:Play()
		Humanoid.WalkSpeed = Values.Walk.Value
	end
end)

You could try to detect if ā€œSā€ is also being pressed while the shift button and w is being pressed, making it an elseif statement.

if KeyPressed(Enum.KeyCode.W) and KeyPressed(Enum.KeyCode.LeftShift) then
			StartRun()
			IsRunning.Value = true
			SprintFOVTween:Play()
			Humanoid.WalkSpeed = Values.Speed.Value
		elseif KeyPressed(Enum.KeyCode.W) and KeyPressed(Enum.KeyCode.LeftShift) and KeyPressed(Enum.KeyCode.S) then

           -- code... code... code...

        else
            -- the other part of the code

Still, it did not fixed that I tried that before.

Alternatively, have a function that turns a boolvalue on or off. OR, check if keypress and Humanoid.Running

2 Likes

Oh yes yes, forgot about that.

1 Like