How to detect if player is already walking and holding shift to sprint

I made a sprint script for my game but whenever the player holds shift it plays the running animation and zooms the screen out as if you were running. How do I fix this?

This is the main function for the script:

UIS.InputBegan:Connect(function(Input: InputObject, Processed: boolean)
	if not Processed then 
		if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key then
			Humanoid.WalkSpeed = Speed
			SprintingTween:Play()
			local Anim = Instance.new('Animation')
			Anim.AnimationId = 'rbxassetid://10741505640'
			PlayAnim = Character.Humanoid:LoadAnimation(Anim)
			PlayAnim:Play()
		end
		end
	
	end)
2 Likes

You can make sure the humanoid’s MoveDirection is greater than 0.

if game.Players.LocalPlayer.Character.Humanoid.MoveDirection.Magnitude > 0 then
    —Sprint code here
end

if Humanoid.MoveDirection.Magnitude > 0 then

Would I put that before or after where it detects the shift key?

After it detects the shift key.

1 Like

Could you not use Humanoid.Running? Or just check the speed if the user is at running speed.

https://developer.roblox.com/en-us/api-reference/event/Humanoid/Running

It sorta worked but if I stop holding W it keeps playing the animation

You can check the MoveDirection of the humanoid if it’s more than 0 (which means walking) and UserInputService:IsKeyDown() to check if ‘Shift’ is being held.

local UIS = game:GetService("UserInputService")
local PS = game:GetService("Players")

local player = PS.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then
		return
	end
	
	if input.KeyCode == Enum.KeyCode.LeftShift and hum.MoveDirection.Magnitude > 0 then
		-- do all running effects
		warn("running")

		repeat task.wait() until not UIS:IsKeyDown(Enum.KeyCode.LeftShift) or hum.MoveDirection.Magnitude <= 0
		
		-- stop all running effects
		warn("not running")
	end
end)
5 Likes

Thanks you so much ! You helped me a lot !

this worked! except, half the time it doesn’t register that the character is moving. help?