How to stop running animation when you jump?

Hello, I currently need help on how to stop an animation if you are jumping.

I need help cause I have a deadline, so if you can tell me how, or just send me the script on how to fix this.

I currently cannot provide a video for this right now, but I can give you guys the script.

  • Script:
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char.Humanoid or char:WaitForChild("Humanoid")

local animTrack = script:WaitForChild("runningAnim")
local loadAnimTrack = hum:LoadAnimation(animTrack)

local default = 6
local sprinting = 15
local onSprinting = false
local onSprint = false
local jumping = false

local UIS = game:GetService("UserInputService")

hum.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.Jumping or new == Enum.HumanoidStateType.Freefall then
		jumping = true
		while jumping == true do
			wait()
			onSprint = false
		end
	end
	if new == Enum.HumanoidStateType.Landed then
		jumping = false
		while jumping == false and onSprinting == true do
			wait()
			onSprint = true
		end
	end
	if new == Enum.HumanoidStateType.Landed then
		jumping = false
		while jumping == false and not onSprinting do
			wait()
			onSprint = false
		end
	end
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.LeftShift then
			while true do wait(0.1)
				if hum.WalkSpeed > 6 then
					onSprinting = true
					onSprint = true
				end
				if hum.WalkSpeed <= 6 then
					onSprinting = false
					onSprint = false
				end
			end
			if hum:GetState() == Enum.HumanoidStateType.Running or hum:GetState() == Enum.HumanoidStateType.RunningNoPhysics then
				if hum.WalkSpeed > 6 and onSprint == true then
					loadAnimTrack:Play()
				elseif hum.WalkSpeed <= 6 and onSprint == false then
					loadAnimTrack:Stop()
				end
			end
		end
	end)
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = sprinting
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = default
	end
end)

I’ll respond to replies tomorrow since I’m going to sleep but,
thanks for helping!

4 Likes

Best way is editing the default roblox animation script, other than that you’re gonna have to deal with situational thinking to create your own state system.

2 Likes

https://developer.roblox.com/en-us/api-reference/event/UserInputService/JumpRequest

2 Likes

The thing is, I am making a shift to sprint script, so I currently can’t.

1 Like

Then is best to utilize Humanoid States, not sure if they have Jumped Connection, but you could browse through the api to create a quick layout.

2 Likes

I have currently tried that in the script but it causes a lot of bugs.

hum.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.Jumping or new == Enum.HumanoidStateType.Freefall then
		jumping = true
		while jumping == true do
			wait()
			onSprint = false
		end
	end
	if new == Enum.HumanoidStateType.Landed then
		jumping = false
		while jumping == false and onSprinting == true do
			wait()
			onSprint = true
		end
	end
	if new == Enum.HumanoidStateType.Landed then
		jumping = false
		while jumping == false and not onSprinting do
			wait()
			onSprint = false
		end
	end
1 Like

Nevermind I fixed it lol, I just switched some things up and it got fixed.

  • Script:
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char.Humanoid or char:WaitForChild("Humanoid")

local cam = workspace.CurrentCamera

local animTrack = script:WaitForChild("runningAnim")
local loadAnimTrack = hum:LoadAnimation(animTrack)

local default = 6
local sprinting = 15
local onSprinting, onSprint, jumping, onFOV = false, false, false, false

local UIS = game:GetService("UserInputService")
local ts = game:GetService("TweenService")

local FOVIn = ts:Create(cam, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {FieldOfView = cam.FieldOfView + 5})
local FOVOut = ts:Create(cam, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {FieldOfView = cam.FieldOfView - 5})

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = sprinting
		onSprinting = true
		onFOV = true
		loadAnimTrack:Play()
		if onFOV == true then
			FOVIn:Play()
		end
		hum.StateChanged:Connect(function(old, new)
			if new == Enum.HumanoidStateType.Jumping or new == Enum.HumanoidStateType.Freefall then
				jumping = true
				if jumping == true then
					wait()
					onSprint = false
					loadAnimTrack:Stop()
				end
			end
			if new == Enum.HumanoidStateType.Landed then
				jumping = false
				if jumping == false and onSprinting == true then
					wait()
					onSprint = true
					loadAnimTrack:Play()
				end
			end
			if new == Enum.HumanoidStateType.Landed then
				jumping = false
				if jumping == false and not onSprinting then
					wait()
					onSprint = false
					loadAnimTrack:Stop()
				end
			end
		end)
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		hum.WalkSpeed = default
		onSprinting = false
		onFOV = false
		loadAnimTrack:Stop()
		if onFOV == false then
			FOVOut:Play()
		end
	end
end)

6 Likes

I know I was late but I have a question, I tried the script and the animation was playing while idle, is there any way to fix this? Im a beginner so I dont really know how to do it.

You probably haven’t implemented the running animation or just used the idle animation.