Something is wrong with my sprint script

So i have a script that when u press left shift u sprint.

When you stand still and press left shift nothing happens but the problem is that when im standing still and press shift and then move then my character isnt sprinting

my script:

local camera = game.Workspace.CurrentCamera
local TweeningService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local Bar = script.Parent:WaitForChild('Border'):WaitForChild("Bar")
local player = game.Players.LocalPlayer



local walk = 16
local run = 24

local power = 10
local sprinting = false
local jumping = false

repeat wait() until game.Players.LocalPlayer.Character

local character = player.Character
local Humanoid = character.Humanoid
local FOVChanges = {
	FieldOfView = 85
}

local TweenInformation = TweenInfo.new(
	1,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

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

local FOVChanges2 = {
	FieldOfView = 70
}

local TweenInformation2 = TweenInfo.new(
	1,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local tween2 = TweeningService:Create(camera, TweenInformation2, FOVChanges2)

UIS.InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false and Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
		character.Humanoid.WalkSpeed = run
		sprinting = true
		while power > 0 and sprinting do
			power = power - .03
			Bar.Size = UDim2.new(power / 10, 0, 1, 0)
			tween:Play()
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = walk
			end
		end
	end
end)

UIS.InputEnded:Connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		character.Humanoid.WalkSpeed = walk
		sprinting = false
		while power < 10 and not sprinting do
			power = power + .03
			Bar.Size = UDim2.new(power / 10, 0, 1, 0)
			tween2:Play()
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = walk
			end
		end
	end
end)

This line here seems to be causing the problem:
if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false and Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) then
one of the conditions for it is that the player is moving. When you are standing still the conditions are not met, so the running script doesn’t actually run

1 Like

I made some changes base off of what @PoppyandNeivaarecute said.
Make sure it’s a local script in StarterCharacterScripts.

I removed the border from the script to make it work for my testing.

local camera = game.Workspace.CurrentCamera
local TweeningService = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer



local walk = 16
local run = 24

local power = 10
local sprinting = false
local jumping = false

repeat wait() until game.Players.LocalPlayer.Character

local character = player.Character
local Humanoid = character.Humanoid
local FOVChanges = {
	FieldOfView = 75
}

local TweenInformation = TweenInfo.new(
	1,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

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

local FOVChanges2 = {
	FieldOfView = 65
}

local TweenInformation2 = TweenInfo.new(
	1,
	Enum.EasingStyle.Quint,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

local tween2 = TweeningService:Create(camera, TweenInformation2, FOVChanges2)

UIS.InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		character.Humanoid.WalkSpeed = run
		sprinting = true
		while power > 0 and sprinting do
			power = power - .03
			tween:Play()
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = walk
			end
		end
	end
end)

UIS.InputEnded:Connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		character.Humanoid.WalkSpeed = walk
		sprinting = false
		while power < 10 and not sprinting do
			power = power + .03
			tween2:Play()
			wait()
			if power <= 0 then
				character.Humanoid.WalkSpeed = walk
			end
		end
	end
end)
1 Like