If I stop moving, sprint still continues

I am trying to make a sprint script that only continues decreasing value while you are moving. I tried and it didn’t work. My code:

UserInputService.InputBegan:connect(function(inputkey)
	if inputkey.KeyCode == Enum.KeyCode.LeftShift then
		if Character.Humanoid.MoveDirection.Magnitude > 0 then
		if (Sprinting == false) and (Stamina.Value > StaminaReductionRate) then
			Sprinting = true
			Character.Humanoid.WalkSpeed = SprintSpeed
				repeat
				wait(StaminaReductionDelay)
				Stamina.Value = Stamina.Value - StaminaReductionRate
			until (Sprinting == false)
				Character.Humanoid.WalkSpeed = OriginalWalkSpeed
			end
		end
	end
end)

and yes, I did try to put another Character.Humanoid.MoveDirection.Magnitude > 0 after the repeat, but it crashes the game whenever I do that.

3 Likes

Firstly, code positions are can create problem. Otherwise, You need to show us the factor thats makes Sprinting == false.

1 Like
UserInputService.InputEnded:connect(function(inputkey)
	if inputkey.KeyCode == Enum.KeyCode.LeftShift then
		if (Sprinting == true) then
			Sprinting = false
			Character.Humanoid.WalkSpeed = OriginalWalkSpeed
		end
	end
end)
1 Like

directly after the inputbegan block of code

Could you post the full script please?

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.Parent.Parent.Parent:WaitForChild('STS_Settings')

local Character = game.Players.LocalPlayer.Character
if Character == nil then
	repeat
		wait()
	until (Character ~= nil)
end
local Stamina = Character:WaitForChild('Stamina')
local Sprinting = false
local StaminaReductionRate = STS_Settings:WaitForChild('StaminaDeductionAmount').Value
local StaminaReductionDelay = STS_Settings:WaitForChild('StaminaDeductionDelay').Value
local OriginalWalkSpeed = Character.Humanoid.WalkSpeed
local SprintSpeed = STS_Settings:WaitForChild('SprintSpeed').Value
local RegenerateDelay = STS_Settings:WaitForChild('StaminaRegenerationDelay').Value
local RegenerateValue = STS_Settings:WaitForChild('StaminaRegenerationAmount').Value

local Stamina_Red_Color = Color3.new(0.635294, 0.32549, 0.0196078)
local Stamina_Yellow_Color = Color3.new(0.72549, 0.376471, 0.0235294)
local Stamina_Green_Color = Color3.new(0.866667, 0.458824, 0.0313725)


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
				for _, child in pairs(character:GetChildren()) do
					if child:IsA('Humanoid') then
						humanoidCache[player] = child
						return child
					end
				end
			end
		end
	end
end

UserInputService.InputBegan:connect(function(inputkey)
	if inputkey.KeyCode == Enum.KeyCode.LeftShift then
		if Character.Humanoid.MoveDirection.Magnitude > 0 then
		if (Sprinting == false) and (Stamina.Value > StaminaReductionRate) then
			Sprinting = true
			Character.Humanoid.WalkSpeed = SprintSpeed
				repeat
				wait(StaminaReductionDelay)
				Stamina.Value = Stamina.Value - StaminaReductionRate
			until (Sprinting == false)
				Character.Humanoid.WalkSpeed = OriginalWalkSpeed
			end
		end
	end
end)

UserInputService.InputEnded:connect(function(inputkey)
	if inputkey.KeyCode == Enum.KeyCode.LeftShift then
		if (Sprinting == true) then
			Sprinting = false
			Character.Humanoid.WalkSpeed = OriginalWalkSpeed
		end
	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
	
	staminaPercent = Util.Clamp(0, 1, staminaPercent)
	local staminaColor = HealthbarColorTransferFunction(staminaPercent)
	Fill.BackgroundColor3 = staminaColor
end)

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

Could you explain the problem a bit more I don’t see any obvious issues with this code. Do you keep losing stamina even after you stop sprinting? What is the value of StaminaReductionDelay?

1 Like

The issue here is where you say

repeat
				wait(StaminaReductionDelay)
				Stamina.Value = Stamina.Value - StaminaReductionRate

			until (Sprinting == false)

It is repeating infinitely because I don’t see where Sprinting is determined as false and is always true.

Unless you have this as a string somewhere else where you can add a function to specify it to == false

Yes, I keep losing stamina after I stop moving.