Animation won't stop when next Animation plays


local Sit = script.Parent
local Animation = Sit:WaitForChild("Sit2")
local OtherAnim = script.Parent.Parent.Sit:WaitForChild("SIt1TempB")
local Temp = false



local Humanoid = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")

local Animator = Humanoid:FindFirstChild("Animator")

local AnimationTrack = Animator:LoadAnimation(Animation)

local AnimationTrack2 = Animator:LoadAnimation(OtherAnim)


Sit.MouseButton1Click:Connect(function()
	
	if AnimationTrack2.IsPlaying then
	AnimationTrack2:Stop()
	end


	if Temp == false then
	Temp = true
		

			AnimationTrack:Play()
		
elseif Temp == true then
		Temp = false
		
			AnimationTrack:Stop()
	

			end
			end)


I made a script that plays an animation when you click GUI, and stops when you click it again. That works, the problem is people can play both animations at once,

which is something I don’t want. I’m trying to make it so if one animation is playing, the other stops playing. I’m stumped on how to fix it and need abit of help

I would store the currently playing animation in a variable, and whenever you change the animation, stop the “currently playing” animation and swap it out for the new one

I’m not sure what you mean? Can you give an example?

local currentlyPlaying = nil
function changeAnimation(newAnimation)
    if currentlyPlaying then currentlyPlaying:Stop() end
    currentlyPlaying = newAnimation
    currentlyPlaying:Play()
end

I’m still confused, whats the function for changeAnimation?

it’s pseudocode I’m showing you how to do it not writing code for you