Does loading a new animation track every time cause memory leaks?

I have a little animation function that will load a track by name into a AnimationController every time that animation is played, as the topic title states, would this cause memory leaks?

I do it this way because I believe the API only allows us to get playing tracks, not previously played tracks.

here is the code I am using

function ManageStand.PlayAnimation(params, animationName, animationSpeed)

	local animationLength
	local playerStandFolder = workspace.PlayerStands:FindFirstChild(params.InitUserId)
	local targetStand = playerStandFolder:FindFirstChildWhichIsA("Model")
	
	local animationController = targetStand:FindFirstChild("AnimationController")
	if animationController then
		local thisAnimation = ReplicatedStorage.StandAnimations:FindFirstChild(animationName)
		if thisAnimation then
			local newTrack = animationController:LoadAnimation(thisAnimation)
			if animationSpeed then
				newTrack:AdjustSpeed(animationSpeed)
			end
			newTrack:Play()
			animationTime = newTrack.Length
		end
	end

	return animationLength
end

No, because there’s a limit of 256 active animation tracks per animation controller. However this is still an issue for you since that means after a while nothing gets animated anymore.

Why are you creating a new track every time? You can save the track in a variable and if the variable is nil you can create a new track.

2 Likes

I load them fresh every time because these animations are not being played on the players character, they are played on Stands which are kinda like pets. During a play session, a player might swap their stand 20 times or more and these stands are completely destroyed when they are removed every time, so therefore the animation controller is also destroyed at this time as well.