How to detect if an animation is playing?

I want to make a player do an animation loop when a button is pressed, and cancel when the button is pressed again.

The issue is that the script cannot tell if an animation is playing.

The script is a normal script and I have tried using .IsPlaying and :IsPlaying() both in Booleans that are in a while true do loop but I cant get it to work so I had started my script back.

Script:

local Button = script.Parent
local Emote = Button["Spin Emote"] -- Insert Emote
local EmoteOn = false

local StarterPack = game:GetService("StarterPack")
local fists = game.StarterPack.Fists

local rs = game:GetService("ReplicatedStorage")
local spin = rs:FindFirstChild("HumanoidToEmotes")

		spin.OnServerEvent:Connect(function(player, Humanoid)
		Button.MouseButton1Down:Connect(function()	
			local LoadEmote = Humanoid:LoadAnimation(Emote)
			
			if EmoteOn == false then
			LoadEmote:Play()
		end
	end)
end)

Does anyone know how i can do this?

Is this a LocalScript instead of just normal Script?

When you deal with Animations, especially if it’s on Player’s character, it must be on ClientSide only.

In your scenario you can get the active animations by doing:

Humanoid.Animator:GetPlayingAnimationTracks()

This function returns a table with all playing tracks being run on that specific animator.