Finding an Animation in a player?

I’m struggling to find an animation playing in the player once i fire another event, so when “Handcuffed” event is fired, it creates a new animation and plays it in the player that is cuffed, which is “p”. My issue is finding that animation in p and destroying it. Any help is apperciated.

Code:

local rs = game:GetService("ReplicatedStorage")
local folder = rs:WaitForChild("Remotes")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		repeat wait() until char.Head
		local newValue = Instance.new("BoolValue",char)
		newValue.Name = "HandcuffValue"
		newValue.Value = false
		char.Parent = game.Workspace:WaitForChild("Players")
	end)
end)
folder.Handcuff.OnServerEvent:Connect(function(player,p)
	p.HandcuffValue.Value = true
	p.Humanoid.WalkSpeed = 0
	
local CuffedAnimation = Instance.new("Animation")

CuffedAnimation.AnimationId = "rbxassetid://5295786801"
	
 
local CuffedTrack = p.Humanoid:LoadAnimation(CuffedAnimation)

CuffedTrack:Play()
	
end)

folder.Uncuff.OnServerEvent:Connect(function(player,p)
	p.HandcuffValue.Value = false
	p.Humanoid.WalkSpeed = 16
	
	
	local AnimationTracks = p.Humanoid:GetPlayingAnimationTracks()
	for i, track in pairs (AnimationTracks) do
		if track.Name == "CuffedTrack" then
			track:Destroy()
		end
	
end

	

end)

Am I missing something, or are you not elaborating your issue? When you destroy the animation track, will it stop, not find the animation, or freeze the player in that position? Put a print command after it destroys to see if it actually finds it. If not, it most likely cannot find an animation with the given name.

Also there is no point for sending an additional argument. It already sends the player argument, so just assign a variable for the player’s character.

folder.Handcuff.OnServerEvent:Connect(function(player)

local p = player.Character
-- continue code
folder.Uncuff.OnServerEvent:Connect(function(player)

local p = player.Character
-- continue code

p = the player that is cuffed, it’s being sent from a local script that detects when you press F within 8 studs of a player.

What i’m trying to do is access p from the code i sent, which i achieve firing the first “Handcuff” event and insert the animation.

But when i Uncuff is fired, i cannot figure out how to access the animation and stop it from playing then destroying it.

Instead of creating a new animation every time, you can have one in the server storage and use that same animation for every player. And instead of destroying the animation, you can use :Stop().

How should i go about that, just cloning it into the player?

Place an animation in the server storage named CuffedAnimation or whatever you’d like, enter its asset id (using studio and not code), and instead of using
local CuffedAnimation = Instance.new(“Animation”)
use:
local CuffedAnimation = game:FindFirstChild(“ServerStorage”):WaitForChild(“CuffedAnimation”)

and instead of using
track:Destroy()
use:
track:Stop()

Edit: No cloning required.

I’m just a bit confused on how im putting this into the player?

local tracks = Humanoid:GetPlayingTracks()
for i,v in pairs(tracks) do print(i,v) end

You can get the currently playing tracks for the humanoid if you’re using a completely different script to get the specific track. The trick is inside of the for loop you check if v, or i (don’t know haven’t tested) are the specific track that you want to stop playing.

An alternative for using the same track would be this:

local track
trackAnim = game.ServerStorage:WaitForChild("Animation",2)

if trackAnim then
track = Humanoid:LoadAnimation(trackAnim)
track:Play()
wait(track.Length)
track:Stop()
end

These variables would go at the top of your script, so they override any other variables named the same.