How to make it so sprinting doesn't work if player isn't moving?

I have this stamina bar script with an animation. It looks funny and is a waste of stamina if the player holds down Shift to sprint, but they are just walking in place and their stamina reduces. How can I stop this? I’m thinking of detecting if the player is pressing WASD, but I’m not sure if thats a thing. Help?

local UIS = game:GetService('UserInputService')
local Bar = script.Parent:WaitForChild('Background'):WaitForChild('Bar')
local StarterGui = game:GetService("StarterGui")

local Player = game.Players.LocalPlayer
local Character = Player.Character

local NormalWalkSpeed = 12
local NewWalkSpeed = 19
local Power = 10
local PowerIncrease = script.Parent.PowerAddValue.Value  -- the amount of power being replenished when not running

local sprinting = false
local IsSprinting = script.Parent:WaitForChild("IsSprinting")


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

local Anim = Instance.new('Animation')
Anim.AnimationId = 'rbxassetid://913376220'
RunAnim = Character.Humanoid:LoadAnimation(Anim)

UIS.InputBegan:connect(function(key, gameProcessed) 
	
	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		
		RunAnim:Play()
	
		Character.Humanoid.WalkSpeed = NewWalkSpeed
		
		Character.Crouch.Disabled = true
		
		sprinting = true
		
		IsSprinting.Value = true
		
		while Power > 0 and sprinting do
		
			Power = Power - 0.035 -- the amount of stamina decreased when running
			
			Bar:TweenSize(UDim2.new(Power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)
			
			wait()

			if Power <= 0 then -- less than or equal to 0
				IsSprinting.Value = false
				Character.SoundPart.HeavyBreathing:Play()
				RunAnim:Stop()
				Bar.BackgroundColor3 = Color3.fromRGB(203, 0, 0) -- changes color to red
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end
end)


UIS.InputEnded:connect(function(key, gameProcessed)

	if key.KeyCode == Enum.KeyCode.LeftShift and gameProcessed == false then
		
		RunAnim:Stop()
		
		Character.Humanoid.WalkSpeed = NormalWalkSpeed
		
		Character.Crouch.Disabled = false
		
		sprinting = false
		
		IsSprinting.Value = false
		
		while Power < 10 and not sprinting do
			
			RunAnim:Stop()
			
			Power = Power + PowerIncrease 

			Bar:TweenSize(UDim2.new(Power / 10, -1, 0.6, 4), 'Out', 'Quint', .05, true)
			
			wait()
			
			if Power <= 3 then -- less than or equal to 3
				RunAnim:Stop()
				sprinting = false
				IsSprinting.Value = false
				Character.Humanoid.WalkSpeed = NormalWalkSpeed
			end
		end
	end	
end)


local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	if Power >= 3 then -- greater than or equal to 3
		Bar.BackgroundColor3 = Color3.fromRGB(0, 0, 0)
		Character.SoundPart.HeavyBreathing:Stop()
	end	
end)

RunService.RenderStepped:Connect(function()
	if Power <= 3 then -- less than or equal to 3
		Bar.BackgroundColor3 = Color3.fromRGB(203, 0, 0)
	end	
end)

You can check to see if the magnitude of the Humanoid’s MoveDirection is greater than zero, like this:

if Character.Humanoid.MoveDirection.Magnitude > 0 then
	--// Put your sprinting code here
end

MoveDirection is a Vector3 property of a Humanoid object. When the player isn’t moving, this property will be equal to 0, 0, 0, therefore the magnitude will be zero. But when the player is moving, even if it is ever so slightly, it will be x, y, z where either or all x, y, and z are greater than zero, therefore the magnitude will be greater than zero. And if the magnitude is greater than zero, you can determine that the player is moving.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.