Animation not Stopping with :Stop()

Hello, I’m having a small issue with animations playing. I have an idle animation playing right now using MoveDirection to detect when the player isn’t moving. When it detects such, it will load and play the idle animation, but it should stop when the player moves again, which it doesn’t. Any help?

Just noticed that I didn’t tell the script to stop when the player moves, but the idle animation still plays even when the CurrentMode is changed

function SetAnimations()
	if char.Humanoid.MoveDirection == Vector3.new(0,0,0) then
		if Modes[CurrentMode] ~= nil then
			for i, v in pairs(Modes) do
				if Modes[Idle] then
					if Modes[CurrentMode].Name == v.Name then
						Idle.AnimationId = v[Idle]
						IdleTrack = Animator:LoadAnimation(Idle)
						IdleTrack:Play()
						print("Idle Playing")
					end
				end
			end
		end
	else
		if IdleTrack ~= nil then
			IdleTrack:Stop()
		end
	end
end

There’s no errors in the output or anything of the sort.

2 Likes

Nevermind, this was quickly resolved after further testing, is it possible to delete this thread?

Scratch that, it isn’t working after updating the code to be more optimised, any help?

The function is fired every time the player presses E,Q or if their MoveDirection is changed, if that information was needed.

Instead of using custom scripts to handle an idle animation, can’t you just replace Roblox’s default one?

There’s a code section for it in the documentation page above:

local Players = game:GetService("Players")

local function onCharacterAdded(character)
	-- Get animator on humanoid
	local humanoid = character:WaitForChild("Humanoid")
	local animator = humanoid:WaitForChild("Animator")

	-- Stop all animation tracks
	for _, playingTrack in animator:GetPlayingAnimationTracks() do
		playingTrack:Stop(0)
	end

	local animateScript = character:WaitForChild("Animate")
	animateScript.idle.Animation1.AnimationId = "rbxassetid://"
	animateScript.idle.Animation2.AnimationId = "rbxassetid://"
end

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

Players.PlayerAdded:Connect(onPlayerAdded)

Since this is for one out of a few other classes, it’ll later be working with animations outside of idle / movement animations which is why I did it on a custom one. it’ll also not be applicable to every player

Could you show your updated code?

The original post’s code had its old code replaced before unmarking the solution, Apologies, I should’ve said so. the current code above is the updated one

Upping this, if anyone needs it again, this is the current code

function SetAnimations()
	for i, v in pairs(Modes) do
		if Modes[CurrentMode] then
			if Modes[CurrentMode].Name == v.Name then
				if v.IdleAnim then
					if char.Humanoid.MoveDirection == Vector3.new(0,0,0) then
						Idle.AnimationId = v.IdleAnim
						IdleTrack = Animator:LoadAnimation(Idle)
						if IdleTrack.IsPlaying then
							print("Reloading anim")
							IdleTrack:Stop()
							IdleTrack:Play()
						else
							IdleTrack:Play()
						end
					else
						IdleTrack:Stop()
					end
				else
					IdleTrack:Stop()
				end
			end
		end
	end
end


This is what’s happening, the sword animation works fine and stops when intended. However, when I switch to the claws, the idle animation for the sword becomes the movement animation (?) and the claw becomes the idle. If I disable the claws, then the sword becomes both idle and movement animations. If I go back to the sword after this, the claw animation is there instead. I don’t get why, they’re both set to the Idle priority too.