Help with sprint script

Hello! Need some help with my sprint script.
When i jump it wont stop the running animation.

local userInputService = game:GetService("UserInputService")

local stamina = script.Parent:WaitForChild("Stamina")

local humanoid = script.Parent:WaitForChild("Humanoid")

local runAnim = humanoid:LoadAnimation(script.RunAnim)

local isSprinting = false

local camera = workspace.CurrentCamera

local tweenService = game:GetService("TweenService")

local tInfo = TweenInfo.new(0.4, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)

local tFovSprint = tweenService:Create(camera, tInfo, {FieldOfView = 100})

local tFovWalk = tweenService:Create(camera, tInfo, {FieldOfView = 90})

local runService = game:GetService("RunService")

userInputService.InputBegan:connect(function(input, gameProcessing)

if not gameProcessing then

if input.KeyCode == Enum.KeyCode.LeftShift then

if stamina.Value > 0 then

if humanoid.MoveDirection.Magnitude > 0 then

isSprinting = true

script.SprintEvent:FireServer(true)

tFovSprint:Play()

end

end

end

end

end)

userInputService.InputEnded:connect(function(input)

if input.KeyCode == Enum.KeyCode.LeftShift then

isSprinting = false

script.SprintEvent:FireServer(false)

runAnim:Stop()

tFovWalk:Play()

end

end)

humanoid.Running:connect(function(speed)

if speed > 0 then

if isSprinting then

if not runAnim.IsPlaying then

runAnim:Play()

script.SprintEvent:FireServer(true)

end

end

else

runAnim:Stop()

script.SprintEvent:FireServer(false)

end

end)

stamina.Changed:connect(function()

if stamina.Value <= 0 then

isSprinting = false

script.SprintEvent:FireServer(false)

runAnim:Stop()

end

end)

while true do

wait(0.1)

if stamina.Value == 25 then

script.Breath:Play()

elseif stamina.Value >= 35 then

script.Breath:Stop()

end

if stamina.Value >= 125 then

stamina.Value = 125

end

end

runService.RenderStepped:Connect(function()

if isSprinting == true then

if humanoid.FloorMaterial == Enum.Material.Air then

if runAnim.IsPlaying then

runAnim:Stop(0.1)

end

elseif humanoid.FloorMaterial ~= Enum.Material.Air and humanoid.MoveDirection.Magnitude > 0 then

if not runAnim.IsPlaying then

runAnim:Play(0.25)

end

end

end

end

Also would be thankful for inplementing function (if any tool equpied play different sprint animation)

1 Like

It looks like you are manually setting the animation, so when you jump it doesn’t automatically change as the HumanoidStateType changes.
It might be better to base the animation playing based upon a mixture of Walkspeed and HumanoidStateType
Alternatively, you could do this as a quick fix:

humanoid.StateChanged:Connect(function(oldState, newState)
	if newState == Enum.HumanoidStateType.Jumping then
		runAnim:Stop(0.1)
	end
end)
1 Like