Animation Override?

Hello all!

I have a quick question.
How would I install an animation override script into my game, basically when you press specific buttons, Example WASD? and when you press each button an animation plays but when you press a button the instantly press the other it stops the previous animation.

1 Like
local animator = if Humanoid then Humanoid:FindFirstChildOfClass("Animator") else nil;
if animator then
	local animTracks = animator:GetPlayingAnimationTracks()
	for i,track in ipairs(animTracks) do
		if track.Name == "NameOfAnimYouWantToStop" then 
			track:Stop(0); 
		end
	end

	if not animationTrackToPlay.IsPlaying then
		-- Play the new animation if it is not playing
		animationTrackToPlay:Play();
	end

end

It did not work, would you like a snippet of my code to understand the problem better?

It may help me a little, I assumed you were trying to animate the humanoid.

alright here

local animations = {
	[Enum.KeyCode.W] = "KnockbackUp",
	[Enum.KeyCode.A] = "KnockbackLeft",
	[Enum.KeyCode.S] = "KnockbackDown",
	[Enum.KeyCode.D] = "KnockbackRight",
}

tool.Equipped:Connect(function()
	local Character = tool.Parent
	local hmr = Character.HumanoidRootPart

	game.Players.LocalPlayer.PlayerGui.FnfGui.Enabled = true

	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = hmr.CFrame + hmr.CFrame.RightVector * 13
	camera.CFrame = CFrame.lookAt(camera.CFrame.Position, (hmr.CFrame + hmr.CFrame.LookVector * 5).Position)

	Character.Humanoid.WalkSpeed = 0
	Character.Humanoid.JumpPower = 0

	connectionBegan = userInputService.InputBegan:Connect(function(input, processed)
		if processed == true then return end
		if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
		if pressed[input.KeyCode] == nil then return end
		pressed[input.KeyCode] = true
		local animation = animationModule.GetAnimation(animations[input.KeyCode])
		while pressed[input.KeyCode] == true do
			print("Playing:", animations[input.KeyCode])
			highlightImage(game.Players.LocalPlayer.PlayerGui.FnfGui.Game.GlowArrows:FindFirstChild(highlight[input.KeyCode].. "Arrow"),"In")
			animation:Play(0)
			hudScale()
			task.wait()
		end
	end)
	connectionEnded = userInputService.InputEnded:Connect(function(input, processed)
		if processed == true then return end
		if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
		if pressed[input.KeyCode] == nil then return end
		highlightImage(game.Players.LocalPlayer.PlayerGui.FnfGui.Game.GlowArrows:FindFirstChild(highlight[input.KeyCode].. "Arrow"),"Out")
		pressed[input.KeyCode] = false
	end)
end)

This isn’t the full script but there’s a module that loads the animations.

Does the print get fired?

Why is the track being played in a loop? you have a task.wait() but no duration so this will continue on the next step i.e almost continuously it will be calling animation:Play().

There is also no animation:Stop() either, so they may be getting cued or the priority is getting mangled.

theres a loop being if you hold it down it will continue to play the animation.

And its because it finishes the animation. I’ll show u a video


I am holding the keys in the first part of the video. but when I’m just pressing you can see the other animation will not play and override the previous

You should probably wait until one cycle of the animation is finished before playing it again using

animation.Stopped:wait();

Or you should be stopping it manually before playing it again. What is the problem if it plays okay? Does it not switch to a new animation?

no no no, it overrides the current animation and plays the next one. It doesn’t wait until it stops, It completely stops the current one if you press another key that plays an animation

found it, I didn’t lay something right in the script.