Stuck on a part with my animation script

I am attempting to make my own animation GUI, and the animation wont stop when you press the button again, even though the print is playing.


local plr = game.Players.LocalPlayer


local count = 0

for _,Buttons in pairs(script.Parent:GetChildren()) do
	if Buttons:IsA("TextButton") then
		Buttons.MouseButton1Click:Connect(function()
			local Animation = Buttons.Name
			local Replicated = game.ReplicatedStorage.Animations
			
			if Replicated:FindFirstChild(Animation) then
				local Animator = plr.Character.Humanoid.Animator
				
				local play = Animator:LoadAnimation(game.ReplicatedStorage.Animations:FindFirstChild(Animation))
				
				if count == 0 then
					print("Animation Loaded")
					count = count + 1
					play:Play()
				elseif count == 1 then
					count = 0
					play:Stop()
					print("Animation Stopped.")
				end
				
			end
		end)
	end
end

Is play looped?

ignore this

Yes the animation is looped

ignore ignore

Would it be doing this straight after?

local play = Animator:LoadAnimation(game.ReplicatedStorage.Animations:FindFirstChild(Animation))

That’s because you’re loading a new track instance each time a button is clicked, load the animation instance once outside of the loop.