Cancelling out character run animation

Hi there,

I am trying to cancel out the character’s run animation in my game. I want to have a run animation play when the player’s speed is above a certain level, but it doesn’t seem to be working. I have included the scripts that I am using for reference below.

I have tried using the GetPlayingAnimationTracks method as well as other methods, but none of them have worked. Here is the code I am using:

(ServerScriptService)

local function onCharacterAdded(Character)
	
	local Humanoid = Character:WaitForChild("Humanoid")
	local Animator = Humanoid:WaitForChild("Animator")
	
	for _, playingTrack in pairs(Animator:GetPlayingAnimationTracks()) do
		playingTrack:Stop(0)
	end
	
	local AnimateScript = Character:WaitForChild("Animate")
	AnimateScript.run.RunAnim.AnimationId = "rbxassetid://12007458336"
end

local function onPlayerAdded(player)
	player.CharacterAppearanceLoaded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

(StarterPlayerScripts)

The idea was to try to implement something under the Humanoid.Running function here to cancel the run animation and change the asset ID. It currently partly works but you have to jump to cancel out the animation, and I don’t want that to be required.

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

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera

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

local DefaultRunSpeed: number = 30
local DefaultActionKey: Enum.KeyCode = Enum.KeyCode.LeftShift
local DefaultTweenSpeed: number = 0.4

local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(DefaultTweenSpeed), { FieldOfView = DefaultFieldOFView + (DefaultRunSpeed / 5) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(DefaultTweenSpeed), { FieldOfView = DefaultFieldOFView })
local AnimateScript = Character:WaitForChild("Animate")
local Players: Players = game:GetService("Players")
local TweenService: TweenService = game:GetService("TweenService")
local UserInputService: UserInputService = game:GetService("UserInputService")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid: Humanoid = Character:WaitForChild("Humanoid")
local Camera: Camera = workspace.CurrentCamera

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

local DefaultRunSpeed: number = 30
local DefaultActionKey: Enum.KeyCode = Enum.KeyCode.LeftShift
local DefaultTweenSpeed: number = 0.4

local SprintingTween: Tween = TweenService:Create(Camera, TweenInfo.new(DefaultTweenSpeed), { FieldOfView = DefaultFieldOFView + (DefaultRunSpeed / 5) })
local WalkingTween: Tween = TweenService:Create(Camera, TweenInfo.new(DefaultTweenSpeed), { FieldOfView = DefaultFieldOFView })
local AnimateScript = Character:WaitForChild("Animate")

Humanoid.Running:Connect(function(Speed)
	
	local AnimationTracks = Character:WaitForChild("Humanoid"):GetPlayingAnimationTracks()

	if Speed > 16 then
		if AnimateScript.run.RunAnim.AnimationId ~= "rbxassetid://12007452724" then
			AnimateScript.run.RunAnim.AnimationId = "rbxassetid://12007452724"
		end
	elseif Speed > 0 then
		if AnimateScript.run.RunAnim.AnimationId ~= "rbxassetid://12007458336" then
			AnimateScript.run.RunAnim.AnimationId = "rbxassetid://12007458336"	
		end	
	end 

end)

UserInputService.InputBegan:Connect(function(Input: InputObject, Processed: boolean)
	if not Processed then 
		if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == DefaultActionKey then 
			Humanoid.WalkSpeed = DefaultRunSpeed
			SprintingTween:Play()
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input: InputObject)
	if Input.UserInputType == Enum.UserInputType.Keyboard and Input.KeyCode == DefaultActionKey then 
		Humanoid.WalkSpeed = DefaultWalkingSpeed
		WalkingTween:Play()
	end
end)

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

I have also tried using the Stop method on the Humanoid object, but that hasn’t worked either.

I have looked for solutions on the Developer Hub, but haven’t been able to find anything that has helped.

If you have any ideas on how I can get the run animation to play correctly, please let me know. Thanks!

You should have set the condition of Speed > 0 to Speed > 0.01, this is what they actually did for the Animate script as well to control the player’s walk animation and see if that fixes the issue.

The reason Speed > 0 wouldn’t work (I believe) is because even if you’re not moving, your character still has a slight velocity drag after it stops and the humanoid’s running connection doesn’t fire after the drag so…

Also, side note; Playing animations from the Humanoid is NOT recommended. If you were previously using the Animator to load the animations, please revert to that.

I think the problem is that your only changing the id of the animation, this wouldn’t work because Roblox is only going to the play the animation again once you press w again and even when it does hit the speed required for the condition you would have to stop moving and move again for the animation to change. But that obviously wont work because your speed will get reset. So try playing the animation yourself with the :Play function and see what happens.

It seems that the suggestion made by @BappoSama identifies the issue at hand. However, the question remains as to what modifications are necessary to utilize the :Play function instead. Additionally, it is important to consider whether it is necessary to fully cancel out the character animations in order for this to work, as they may override the desired effect if not properly managed. I’m not quite sure, do you guys have any suggestions?