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!