Tool idle/walk animation stops

Hello,
I’m trying to have an animation play whenever a character is idle/moving with a tool equipped. However, the animation doesn’t loop; it only plays once and then back to the default idle/moving animations.
I have set the idle animation priority to idle, and the movement animations to action. I have looked at dev hub to try and solve this. . . I’ve either missed something on the dev hub, or just messed up in the code. I would appreciate if someone could answer this soon :blush:

local humanoid = nil
local char = nil
local player = nil
local rootPart = nil
local mouse = nil
local idleAnim = nil
local forwardAnim = nil
local LeftAnim = nil
local RightAnim = nil
local wPressed = false
local sPressed = false
local aPressed = false
local dPressed = false
local spPressed = false
local equipped = false
local flying = false
local cam = workspace.CurrentCamera
local uis = game:GetService("UserInputService")

script.Parent.Equipped:Connect(function()
	humanoid = script.Parent.Parent.Humanoid
	player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
	char = script.Parent.Parent
	idleAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("IdleAnim"))
	forwardAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ForwardAnim"))
	RightAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ForwardAnim"))
	LeftAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild("ForwardAnim"))
	rootPart = script.Parent.Parent.HumanoidRootPart
	mouse = player:GetMouse()
	equipped = true
	print("anim to be played")
	idleAnim:Play()

end)

script.Parent.Unequipped:Connect(function()
	equipped = false
	idleAnim:Stop()
	print("anim stopped")
	forwardAnim:Stop()
	LeftAnim:Stop()
	RightAnim:Stop()
end)

uis.InputBegan:Connect(function(key, chat)
	if equipped then
		if chat then return end	
		if key.KeyCode == Enum.KeyCode.W and equipped == true then
			wPressed = true

			forwardAnim:Play()
			

		elseif key.KeyCode == Enum.KeyCode.S and equipped == true then
			sPressed = true
			forwardAnim:Play()
			

		elseif key.KeyCode == Enum.KeyCode.A and equipped == true then
			aPressed = true

			LeftAnim:Play()
			

		elseif key.KeyCode == Enum.KeyCode.D and equipped == true then
			dPressed = true

			RightAnim:Play()
			
				

		end
	end

end)

uis.InputEnded:Connect(function(key, chat)
	if chat then return end
	if key.KeyCode == Enum.KeyCode.W then
		wPressed = false
		forwardAnim:Stop()
	elseif key.KeyCode == Enum.KeyCode.S then
		sPressed = false
		forwardAnim:Stop()
	elseif key.KeyCode == Enum.KeyCode.A then
		aPressed = false
		LeftAnim:Stop()
	elseif key.KeyCode == Enum.KeyCode.D then
		dPressed = false
		RightAnim:Stop()
	end
end)

YouTube vid showcasing issue above

Love the video title lol. Anyway, looking at your code, the issue seems to be related to the animations stopping as soon as the key press action ends. Essentially what’s happening is each time you release the key, the animation stops playing, regardless of whether the character is still in motion or not.

You should update your algorithm so that you check the state of the character after you stop pressing a key and maintain the animation as long as the character is still moving.

Since the animations are loaded once the tool equipped, instead of stopping and starting the animations whenever a key is pressed or released, you can adjust the Animation’s AnimationTrack Weight or Speed to 0 when the tool is unequipped or when respective key is released and set them back to 1 when the tool is equipped or when respective key is pressed.

I wanna also remind you that handling multiple running animations for a character might turn tricky when and might override each other based on the animation’s priority. You may want to make sure only one animation runs at a time for simplicity.

Alternatively, you can use AnimationController and AnimationTrack rich functionality in order to layer your animations. You might wanna take a look at these links:

Good luck on figuring it out, reply to me if you need help further.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.