Sprint Animation playing even after landing [SOLVED]

Issues

I’ve written a script based on the one from Claasgreeneye. I have modified it so the run animation stops when you are jumping.
However, I am stuck with this problem. When you aren’t holding shift and jump then land while walking. The sprinting animation will play for no reason. This can be cancel out by pressing shift or sprinting again. It works normally if you keep holding shift after jumps and landings.

So far, I have tried to mess around with the animation priority already but that doesn’t work too.
Video:

this is the code so far:

--< Services >--
local Players: Players = game:GetService("Players")
local TweenService: TweenService = game:GetService("TweenService")
local UIS: UserInputService = game:GetService("UserInputService")

--< Constants >--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera
local onSprinting, onSprint, jumping = false, false, false

local DefaultFieldOFView = Camera.FieldOfView
local DefaultWalkingSpeed = Humanoid.WalkSpeed

--< Variables >--
local Speed: number = 20 -- How fast is the sprinting speed?
local Key: Enum.KeyCode = Enum.KeyCode.LeftShift -- Activation Key
local TweenSpeed: number = 0.6 -- How fast does the field of view change?
local SprintAnimation = script:WaitForChild("sprintAnim")
local Animator = Humanoid:FindFirstChildOfClass("Animator")
local SprintAnimTrack = Animator:LoadAnimation(SprintAnimation)

--< Main Code >--
local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView + (Speed / 5) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView })

UIS.InputBegan:Connect(function(Input: InputObject, Processed: boolean)
	if not Processed then 
		if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key then 
			if Humanoid.MoveDirection.Magnitude > 0 then
				SprintAnimTrack:Play()
				SprintingTween:Play()
				Humanoid.WalkSpeed = Speed
				onSprinting = true

				Humanoid.StateChanged:Connect(function(old, new)

					--< Stop Sprinting Anim when jumping >--

					if new == Enum.HumanoidStateType.Jumping or new == Enum.HumanoidStateType.Freefall then
						jumping = true
						if jumping == true then
							task.wait()
							onSprint = false
							SprintAnimTrack:Stop()
						end
					end

					if new == Enum.HumanoidStateType.Landed then
						jumping = false
						if jumping == false and onSprinting == true then
							task.wait()
							onSprint = true
							SprintAnimTrack:Play()
						end
					end

					if new == Enum.HumanoidStateType.Landed then
						jumping = false
						if jumping == false and onSprinting == false then
							task.wait()
							onSprint = false
							SprintAnimTrack:Stop()
						end
					end
				end)
			
				--<----------------------------------------------- >--
			end
		end
	end
end)

UIS.InputEnded:Connect(function(Input: InputObject)
	if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key then 
		SprintAnimTrack:Stop()
		WalkingTween:Play()
		Humanoid.WalkSpeed = DefaultWalkingSpeed
	end
end)

--< Player Resetting >--
Player.CharacterAdded:Connect(function(Char)
	Humanoid = Char:FindFirstChildWhichIsA("Humanoid") or Char:WaitForChild("Humanoid")
end)

i may not be an expert in coding, but thank you in advance!

2 Likes

Currently you keep registering a new event on humanoid changed, everytime a new input is processed
Try registering the event outside of that method so that it only registers once. As you are setting the onSprinting Variable to true on every processed input.

May or may not fix it, but the issue is definitely that your onSprinting variable is always set to true when you land, so try examining why that would be.

4 Likes

Ah thank you so much! I fixed the code, turns out I didn’t realized that. Before holding shift it detects if you are sprinting or not. If you land while sprinting is equal to false it would play the animation so every times you land it just proceeds to moonwalking.

Fixed Code:

--< Services >--
local Players: Players = game:GetService("Players")
local TweenService: TweenService = game:GetService("TweenService")
local UIS: UserInputService = game:GetService("UserInputService")

--< Constants >--
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera
local onSprinting = false

local DefaultFieldOFView = Camera.FieldOfView
local DefaultWalkingSpeed = Humanoid.WalkSpeed

--< Variables >--
local Speed: number = 20 -- How fast is the sprinting speed?
local Key: Enum.KeyCode = Enum.KeyCode.LeftShift -- Activation Key
local TweenSpeed: number = 0.6 -- How fast does the field of view change?
local SprintAnimation = script:WaitForChild("sprintAnim")
local Animator = Humanoid:FindFirstChildOfClass("Animator")
local SprintAnimTrack = Animator:LoadAnimation(SprintAnimation)

--< Main Code >--
local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView + (Speed / 5) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(TweenSpeed), { FieldOfView = DefaultFieldOFView })

UIS.InputBegan:Connect(function(Input: InputObject, Processed: boolean)
	if not Processed then 
		if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key then 
			if Humanoid.MoveDirection.Magnitude > 0 then
				SprintAnimTrack:Play()
				SprintingTween:Play()
				Humanoid.WalkSpeed = Speed
				onSprinting = true

				Humanoid.StateChanged:Connect(function(old, new)

					--< Stop Sprinting Anim when jumping >--

					if new == Enum.HumanoidStateType.Jumping or new == Enum.HumanoidStateType.Freefall then
						SprintAnimTrack:Stop()
					end

					if new == Enum.HumanoidStateType.Landed and onSprinting == true then
						SprintAnimTrack:Play()
					end
	
				end)
			
				--<----------------------------------------------- >--
			end
		end
	end
end)

UIS.InputEnded:Connect(function(Input: InputObject)
	if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == Key then
		onSprinting = false
		SprintAnimTrack:Stop()
		WalkingTween:Play()
		Humanoid.WalkSpeed = DefaultWalkingSpeed
	end
end)

--< Player Resetting >--
Player.CharacterAdded:Connect(function(Char)
	Humanoid = Char:FindFirstChildWhichIsA("Humanoid") or Char:WaitForChild("Humanoid")
end)
3 Likes

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