How should I go about stopping this animation?

So I’m trying to make this toggle animation switch, and its going fine so far until I have to :Stop() the animation.

local anim = p.Humanoid:LoadAnimation(p.Anim.Sharpen)

	if not d and not t and not alert then

		d = true
		p.Humanoid.WalkSpeed = 0

		alert = true

		for _, v in pairs(child) do

			if v:IsA("TextButton") and v.Name ~= script.Parent.Name then

				v.Visible = false

			end

		end

		script.Parent.Text = retext
		
		anim:Play()

		wait(cd)
		
		anim:Stop()

		t = true
		d = false

	elseif not d and t and alert then

		d = true
		
		anim:Stop()

		alert = false

		for _, v in pairs(child) do

			if v:IsA("TextButton") and v.Name ~= script.Parent.Name then

				v.Visible = true

			end

		end

		p.Humanoid.WalkSpeed = 8

		script.Parent.Text = starttext

		wait(cd)

		t = false
		d = false

	end

This is the part of the script I need help with, and I’ve found out it has to do with the animation

Since whenever I test in game, it shows the button being pressed, then when pressed again being un-pressed. Any idea as to why I can’t stop this animation from playing?

https://gyazo.com/a774cf5ebba8178592cf5780e00f6319

It’s probably the way you assigned your variables, with different conditional checks each checking a certain value

Does the wait(cd) work when stopping the animation after it stops? (You could just use anim.Finished:Wait() if you wanna detect when the animation is finished playing)

the wait(cd) simply waits to change d, which is a debounce. The animation loops constantly as shown in the gif.

local p = game:GetService("Players").LocalPlayer.Character
local d = false
local t = false
local alert = script.Parent.Parent.IsAnimPlaying.Value

local cd = 1

local starttext = script.Parent.Text
local retext = ("Stop")

local child = script.Parent.Parent:GetChildren()

script.Parent.MouseButton1Click:Connect(function()

	local ws = p.Humanoid.WalkSpeed
	
	local anim = p.Humanoid:LoadAnimation(p.Anim.Sharpen)

	if not d and not t and not alert then

		d = true
		p.Humanoid.WalkSpeed = 0

		alert = true

		for _, v in pairs(child) do

			if v:IsA("TextButton") and v.Name ~= script.Parent.Name then

				v.Visible = false

			end

		end

		script.Parent.Text = retext
		
		anim:Play(.4)

		wait(cd)

		t = true
		d = false

	elseif not d and t and alert then

		d = true
		
		anim:Stop()

		alert = false

		for _, v in pairs(child) do

			if v:IsA("TextButton") and v.Name ~= script.Parent.Name then

				v.Visible = true

			end

		end

		p.Humanoid.WalkSpeed = 8

		script.Parent.Text = starttext

		wait(cd)

		t = false
		d = false

	end

end)

heres the full script to prevent confusionly

Edit: I found that using a Stop() in the if statement where i run it, instead of the elseif statement then it works, for some reason.

Well I found out how it works, its because I’m loading the animation via humanoid every time you click it, instead I can just have it load the animation outside of the function it works properly.