Help with LoadAnimation

I’ve worked with animations ages but I’ve never had this issue.
I am working with events so when one person fires an event, the person who is being affected gets the animation played through the server script. Anyways because every time I fire the event, the animation loads AGAIN, the animation doesn’t stop when I do Track:Stop(). Any help would be greatly appreciated.
Below you can find the code and a short gif as my explanation is pretty stupid.

event.OnServerEvent:Connect(function(player, victim, value)
		local detainanimation = victim.Character.Humanoid:LoadAnimation(detainani) -- this part is is the issue as it loads the animation everytime the event fires
		local detainanimation2 = victim.Character.Humanoid:LoadAnimation(detainani2)

	if value == "true" then
	detainanimation:Play() -- this works fine
	wait(1)
	detainanimation2:Play() -- this works fine
else
    print("false") -- this prints  
	detainanimation:Stop() -- it doesnt stop
	detainanimation2:Stop() -- it doesnt stop
end
end)

https://gyazo.com/88fede454dbe41ef73103ccec19be59c

have you thought of stopping it after you play it?

No because I need it to play endlessly until stopped by another event firing.

Your code doesn’t work because when you pass value of false you’re creating new copies of animations, and calling stop on those AnimationTracks, rather than the two tracks that are already playing.

You need to either:

  1. Save references to the two AnimationTracks you started (detainanimation and detainanimation2) in variables whose scope is outside this function (since it’s server side, it might be a table keyed by player instance, or part of a player OOP object if you already have a Lua OOP class representing a player).
  2. Use Humanoid:GetPlayingAnimationTracks() to get all the player’s tracks, then find and stop the two with the animationIds you’re trying to stop.