How To Detect Idling and Jumping

I have this run animation and it’s a simple left shift to sprint but whenever the player is idling or jumping while holding left shift the animation still plays. I want to know how I can update to script to detect idling and jumping so that if the play jumps it plays the jump animation instead and if idling it will play the idle animation. Here is the script that I currently have:

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 35  --run speed
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://15708472541'
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		PlayAnim.Priority = Enum.AnimationPriority.Movement
		PlayAnim:Play()
	end
end)

UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 16  --default walk speed
		PlayAnim:Stop()
	end
end)

1 Like

Check this out

or you can just use Humanoid:GetState()

1 Like

There is documentation on detecting state changes here: Humanoid | Documentation - Roblox Creator Hub

I recommend binding a function to the StateChanged event