Animation does not stop playing

Hello everyone, I’ve come across a problem where the animation does not stop even when there is a :Stop() in the script

local playing = false
local gui = script.Parent.Parent.Parent.DanceGUI
local button = script.Parent.Parent.Parent.Emote

script.Parent.MouseButton1Click:Connect(function()
	local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.HipHop)
	if playing == false then
		gui.Visible = false
		button.Visible = true
		playing = true
		animation:Play()
	else
		if playing == true then
			animation:Stop()
			gui.Visible = false
			button.Visible = true
			playing = false
		end
	end
end)

Can someone help?
(the animation loops btw)

you checked if the animation wasnt playing then put else meaning it would be. You wouldn’t need to check again.

The else is there because you would be playing it, and then you could also stop it. I am a little confused by what you mean.

There would only be two options, either it’s true or false, you wouldn’t need this part:

if playing == true then

Maybe its becuase your loading the animation each time u press the button? and roblox is creating a new value for it instead of keeping the old one. Just a game theory

I removed that part, but all it does now is just replays the animation.

You can try this, you move the local animation variable out of the mouseclick detector

 local playing = false
 local gui = script.Parent.Parent.Parent.DanceGUI
local button = script.Parent.Parent.Parent.Emote
---- here
local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.HipHop)
script.Parent.MouseButton1Click:Connect(function()

if playing == false then
	gui.Visible = false
	button.Visible = true
	playing = true
	animation:Play()
else
	if playing == true then
		animation:Stop()
		gui.Visible = false
		button.Visible = true
		playing = false
	end
end

end)

[/quote]

add this before you start playing the animation:

animation.Looped = false