Sprint Script Wont function correctly

Hello, I’m making a game and I have a sprint system, everything is working except this one problem.

When you hold shift (run button) then stand still and you keep holding then walk again, the run animation will not play again, and you will still be at run speed Aswell. And then once you let go after that happened nothing happens except you letting go.

Source Code:

local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")

local Character = script.Parent.Parent
local Humanoid = Character:FindFirstChildOfClass("Humanoid")

local SprintingTween = TweenService:Create(workspace.CurrentCamera, TweenInfo.new(0.5), {FieldOfView = 75})
local WalkingTween = TweenService:Create(workspace.CurrentCamera, TweenInfo.new(0.5), {FieldOfView = 70})

local Running = false

local Anim = Humanoid:LoadAnimation(script:WaitForChild("RunAnim"))

UserInputService.InputBegan:Connect(function(Input, Processed)
	if Input.KeyCode == Enum.KeyCode.LeftShift and not Processed then
		if (Humanoid.MoveDirection.Magnitude > 0) then
			SprintingTween:Play()
			Running = true
			Humanoid.WalkSpeed = 26
			Anim:Play()
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input, Processed)
	if Input.KeyCode == Enum.KeyCode.LeftShift and not Processed then
		if Running then
			WalkingTween:Play()
			Running = false
			Humanoid.WalkSpeed = 16
			Anim:Stop()
		end
	end 
end)

Humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping and Running then
		Anim:Stop()
	elseif newState == Enum.HumanoidStateType.Landed and Running then
		Anim:Play(0.35)
	end
end)

Humanoid.Running:Connect(function(Speed)
	if Speed > 0 and Running and not Anim.IsPlaying then
		Anim:Play(0.35)
		SprintingTween:Play(0.35)
	elseif Speed <= 16 and Running and Anim.IsPlaying then
		Running = false
		Anim:Stop()
		WalkingTween:Play(0.35)
	end
end)

If you’d like a video for it to explain better, feel free to ask!

1 Like

From what I can tell. You are playing the animation as soon as the player holds shift, what I can recommend, is you make the script when the player walks, instead of pressing shift.

1 Like

I think it’s because you are holding 2 inputs (shift and forward).
If you still hold shift, but let go the forward input then the InputEnded function fires.
You still have shift held, so the if Running loop is still true and the run turns back to a walk.

Will making it
if not Input.KeyCode == Enum.KeyCode.LeftShift and not Processed then
work for you?

2 Likes

Here is one that works for me :

local CoreGuiService = game:GetService(‘CoreGui’)
local PlayersService = game:GetService(‘Players’)
local GuiService = game:GetService(‘GuiService’)
local UserInputService = game:GetService(‘UserInputService’)
local StarterGui = game:GetService(‘StarterGui’)
local STS_Settings = script.Parent:WaitForChild(‘STS_Settings’)
local StaminaButton = script.Parent:WaitForChild(‘Mobile’)
local Border = StaminaButton:WaitForChild(‘Border’)
local Bar = script.Parent:WaitForChild(‘Frame_Bar’)
local SprintGui = script.Parent
local UIS = game:GetService(“UserInputService”)
local TS = game:GetService(“TweenService”)
local plr = game.Players.LocalPlayer
local player = game.Players.LocalPlayer

local Character = game.Players.LocalPlayer.Character
if Character == nil then
repeat
wait()
until (Character ~= nil)
print(‘FoundCharacter’)
end
local Debounce = false
local Stamina = Character:WaitForChild(‘Stamina’)
local Sprinting = false
local StaminaReductionRate = STS_Settings:WaitForChild(‘StaminaDeductionAmount’).Value – Amount taken from Stamina per SprintReductionDelay
local StaminaReductionDelay = STS_Settings:WaitForChild(‘StaminaDeductionDelay’).Value – Time in Seconds for SprintReductionRate to be removed from stamina
local OriginalWalkSpeed = Character.Humanoid.WalkSpeed – Get Original WalkSpeed
local SprintSpeed = STS_Settings:WaitForChild(‘SprintSpeed’).Value
local RegenerateDelay = STS_Settings:WaitForChild(‘StaminaRegenerationDelay’).Value
local RegenerateValue = STS_Settings:WaitForChild(‘StaminaRegenerationAmount’).Value

–// Bar Colors //
local Stamina_Red_Color = Color3.new(255/255, 28/255, 0/255)
local Stamina_Yellow_Color = Color3.new(250/255, 235/255, 0)
local Stamina_Green_Color = Color3.new(27/255, 252/255, 107/255)

local Util = {}
do
function Util.IsTouchDevice()
return UserInputService.TouchEnabled
end

function Util.Create(instanceType)
	return function(data)
		local obj = Instance.new(instanceType)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

function Util.Clamp(low, high, input)
	return math.max(low, math.min(high, input))
end

function Util.DisconnectEvent(conn)
	if conn then
		conn:disconnect()
	end
	return nil
end

function Util.SetGUIInsetBounds(x1, y1, x2, y2)
	GuiService:SetGlobalGuiInset(x1, y1, x2, y2)
end

local humanoidCache = {}
function Util.FindPlayerHumanoid(player)
	local character = player and player.Character
	if character then
		local resultHumanoid = humanoidCache[player]
		if resultHumanoid and resultHumanoid.Parent == character then
			return resultHumanoid
		else
			humanoidCache[player] = nil -- Bust Old Cache
			for _, child in pairs(character:GetChildren()) do
				if child:IsA('Humanoid') then
					humanoidCache[player] = child
					return child
				end
			end
		end
	end
end

end

UIS.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.LeftShift or key.KeyCode == Enum.KeyCode.RightShift then
if (Sprinting == false) and (Stamina.Value > StaminaReductionRate) then
TS:Create(plr.Character.Humanoid, TweenInfo.new(3), {WalkSpeed = SprintSpeed}):Play()
Sprinting = true
repeat
wait(StaminaReductionDelay)
Stamina.Value = Stamina.Value - StaminaReductionRate
until (Sprinting == false)
TS:Create(plr.Character.Humanoid, TweenInfo.new(1.2), {WalkSpeed = OriginalWalkSpeed}):Play()
end
end
end)

UserInputService.InputEnded:connect(function(inputkey)
if inputkey.KeyCode == Enum.KeyCode.LeftShift then
if Sprinting == true then
Sprinting = false
TS:Create(plr.Character.Humanoid, TweenInfo.new(1.2), {WalkSpeed = OriginalWalkSpeed}):Play()
end
end
end)

if UserInputService.TouchEnabled then
StaminaButton.Visible = true
end

StaminaButton.MouseButton1Down:Connect(function()
if (Sprinting == false) and (Stamina.Value > StaminaReductionRate) then
Border.Visible = true
Sprinting = true
TS:Create(plr.Character.Humanoid, TweenInfo.new(3), {WalkSpeed = SprintSpeed}):Play()
repeat
wait(StaminaReductionDelay)
Stamina.Value = Stamina.Value - StaminaReductionRate
until (Sprinting == false)
TS:Create(plr.Character.Humanoid, TweenInfo.new(1.2), {WalkSpeed = OriginalWalkSpeed}):Play()
elseif Sprinting == true and Stamina.Value < 100 then
Border.Visible = false
Sprinting = false
TS:Create(plr.Character.Humanoid, TweenInfo.new(1.2), {WalkSpeed = OriginalWalkSpeed}):Play()
end
end)

Stamina.Changed:connect(function(value)
if (value < StaminaReductionRate) then
Sprinting = false
end
local Bar = script.Parent:WaitForChild(‘Frame_Bar’)
local Fill = script.Parent.Frame_Bar:WaitForChild(‘Frame_BarFill’)
Fill:TweenSize(UDim2.new(value/100,0,1,0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, .5, true)

local healthColorToPosition = {
	[Vector3.new(Stamina_Red_Color.r, Stamina_Red_Color.g, Stamina_Red_Color.b)] = 0.1;
	[Vector3.new(Stamina_Yellow_Color.r, Stamina_Yellow_Color.g, Stamina_Yellow_Color.b)] = 0.5;
	[Vector3.new(Stamina_Green_Color.r, Stamina_Green_Color.g, Stamina_Green_Color.b)] = 0.8;
}
local min = 0.1
local minColor = Stamina_Red_Color
local max = 0.8
local maxColor = Stamina_Green_Color

local function HealthbarColorTransferFunction(staminaPercent)
	if staminaPercent < min then
		return minColor
	elseif staminaPercent > max then
		return maxColor
	end

	local numeratorSum = Vector3.new(0,0,0)
	local denominatorSum = 0
	for colorSampleValue, samplePoint in pairs(healthColorToPosition) do
		local distance = staminaPercent - samplePoint
		if distance == 0 then
			return Color3.new(colorSampleValue.x, colorSampleValue.y, colorSampleValue.z)
		else
			local wi = 1 / (distance*distance)
			numeratorSum = numeratorSum + wi * colorSampleValue
			denominatorSum = denominatorSum + wi
		end
	end
	local result = numeratorSum / denominatorSum
	return Color3.new(result.x, result.y, result.z)
end
local staminaPercent = Stamina.Value / 100

if Stamina.Value == 2 then
	Border.Visible = false
end

staminaPercent = Util.Clamp(0, 1, staminaPercent)
local staminaColor = HealthbarColorTransferFunction(staminaPercent)
Fill.BackgroundColor3 = staminaColor

if Stamina.Value == 100 then
	SprintGui:WaitForChild("Frame_Bar"):TweenPosition(UDim2.new(0.5, 0, 1, SprintGui:WaitForChild("Frame_Bar").AbsoluteSize.Y + 1), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.75, true)
else
	SprintGui:WaitForChild("Frame_Bar"):TweenPosition(UDim2.new(0.5, 0, 1, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quart, 0.25, true)
end

end)

while true do
wait(RegenerateDelay)
if (Sprinting == false) and (Stamina.Value < 100) then
Stamina.Value = Stamina.Value + RegenerateValue
end
end

2 Likes

Thanks for helping, It works now!

3 Likes