Help with Advanced Sprint Script

I have a script which basically has all the things I need (not sprinting until you start moving, tween of running and stopping, animations in the stamina bar and whatnot)

But the thing is, my script has a MAJOR bug, and I don’t know how to fix it. Basically, when the player stops moving and they are still pressing on the Sprint Key, the stamina still depletes.
How do I make it so that whenever they still have their finger on the key but are not moving, the stamina doesn’t deplete?

SCRIPT:

UIS.InputBegan:Connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false and ii > runRef and humanoid.MoveDirection.Magnitude > 0 then
		Bar.BackgroundColor3 = Color3.fromRGB(222, 255, 202)
		TS:Create(player.Character.Humanoid, TweenInfo.new(2), {WalkSpeed = NewWalkSpeed}):Play()
		running = true
		while ii > 0 and running and humanoid.MoveDirection.Magnitude > 0 do
			ii = ii - .025
			Bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), 'Out', 'Quint', 1.5, true)
			wait()
			if ii <= 0 then
				Bar.BackgroundColor3 = Color3.fromRGB(255, 193, 194)
				TS:Create(player.Character.Humanoid, TweenInfo.new(1.25), {WalkSpeed = NormalWalkSpeed}):Play()
			end
			if ii >= runRef then
				Bar.BackgroundColor3 = Color3.fromRGB(222, 255, 202)
			end
			if ii < runRef then
				Bar.BackgroundColor3 = Color3.fromRGB(255, 193, 194)
			end
		end
	end
end)

UIS.InputEnded:Connect(function(key, gameProcessed)
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		TS:Create(player.Character.Humanoid, TweenInfo.new(1.25), {WalkSpeed = NormalWalkSpeed}):Play()
		running = false
		while ii < 10 and not running and humanoid.MoveDirection.Magnitude == 0 do
			ii = ii + .035
			Bar:TweenSize(UDim2.new(ii / 10, 0, 1, 0), 'Out', 'Quint', .5, true)
			wait()
			if ii <= 0 then
				Bar.BackgroundColor3 = Color3.fromRGB(255, 193, 194)
				TS:Create(player.Character.Humanoid, TweenInfo.new(1.25), {WalkSpeed = NormalWalkSpeed}):Play()
			end
			if ii > runRef then
				Bar.BackgroundColor3 = Color3.fromRGB(222, 255, 202)
			else
				Bar.BackgroundColor3 = Color3.fromRGB(255, 193, 194)
			end
		end
	end
end)

You can detect if a player is moving using Humanoid.MoveDirection and getting the length of the Vector3 using the Magnitude of the MoveDirection. You can also take the dot product of the MoveDirection and checking if it is greater than 0.

-- Assuming the humanoid is already defined
Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
    -- Using the magnitude of MoveDirection
    if Humanoid.MoveDirection.Magnitude > 0 then
        print("Player is moving")
    end

    -- Using the dot product is a better method since it saves on unnecessary square root calculations
    if Humanoid.MoveDirection:Dot(Humanoid.MoveDirection) > 0 then
        print("Player is moving")
    end
end)
1 Like

or check if moveDirection ~= Vector3.new(0,0,0)