Sprinting animation playing while in air

If you jump it doesn’t play the sprinting animation but if you walk off a ledge then it does, and idk why. Did I do something wrong? Thanks.

local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local runningAnimation = Instance.new("Animation")
runningAnimation.AnimationId = "rbxassetid://84945600163075"
local isSprinting = false
local runningAnimTrack

local function toggleSprint()
	if isSprinting then
		humanoid.WalkSpeed = 16
		if runningAnimTrack then
			runningAnimTrack:Stop()
		end
	else
		humanoid.WalkSpeed = 35
		if humanoid.MoveDirection.Magnitude > 0 and character.HumanoidRootPart and character.HumanoidRootPart.Material ~= Enum.Material.Air then
			runningAnimTrack = humanoid:LoadAnimation(runningAnimation)
			runningAnimTrack:Play()
		else
			humanoid.Running:Wait()
			if humanoid.MoveDirection.Magnitude > 0 and character.HumanoidRootPart and character.HumanoidRootPart.Material ~= Enum.Material.Air then
				runningAnimTrack = humanoid:LoadAnimation(runningAnimation)
				runningAnimTrack:Play()
			end
		end
	end
	isSprinting = not isSprinting
end

userInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
		toggleSprint()
	end
end)

humanoid.Running:Connect(function(speed)
	if speed == 0 or character.HumanoidRootPart.Material == Enum.Material.Air then
		if isSprinting and runningAnimTrack then
			runningAnimTrack:Stop()
		end
	elseif speed > 0 and isSprinting and not runningAnimTrack.IsPlaying then
		runningAnimTrack:Play()
	end
end)

humanoid.Jumping:Connect(function()
	if isSprinting and runningAnimTrack then
		runningAnimTrack:Stop()
	end
end)

game:GetService("RunService").Heartbeat:Connect(function()
	if character.HumanoidRootPart.Material == Enum.Material.Air and runningAnimTrack then
		runningAnimTrack:Stop()
	end
end)

The problem happens because when you walk off a ledge, Humanoid.Jumping doesn’t fire, but when you press the jump key, it does. Use Humanoid:GetState() instead of checking the floor material. This will let you detect if the player is in the air for any reason (jumping or falling), not just if they hit jumping
Try replace your RunService.Heartbeat block with this:

game:GetService("RunService").Heartbeat:Connect(function()
	local state = humanoid:GetState()
	if (state == Enum.HumanoidStateType.Freefall or state == Enum.HumanoidStateType.Jumping) and runningAnimTrack then
		runningAnimTrack:Stop()
	elseif state == Enum.HumanoidStateType.Running and isSprinting and humanoid.MoveDirection.Magnitude > 0 then
		if not runningAnimTrack or not runningAnimTrack.IsPlaying then
			runningAnimTrack = humanoid:LoadAnimation(runningAnimation)
			runningAnimTrack:Play()
		end
	end
end)
1 Like