Am I cleaning up correctly?

so i made a quick animation wrapper earlier for playing animations. i would love some feedback on whether i’m cleaning up things correctly or not as well in terms of variables, connections, etc. would love any feedback or tips if possible. thanks guys! :slight_smile:

function AnimationPlayer.Play(entity: Instance, AnimationId: number, recycle: boolean?, PlayProperties: {[string]: number}?)
	if not entity then return end
	
	local Humanoid: Humanoid

	if entity:IsA("Player") then
		local Character = entity.Character
		Humanoid = Character and Character:WaitForChild("Humanoid")
	elseif entity:IsA("Model") or entity:IsA("BasePart") then
		Humanoid = entity:FindFirstChildOfClass("Humanoid")
	end

	if not Humanoid then return end

	local Animator: Animator = Humanoid:FindFirstChildOfClass("Animator")
	if not Animator then
		Animator = Instance.new("Animator")
		Animator.Parent = Humanoid
	end

	local Animation = Instance.new("Animation")
	Animation.AnimationId = "rbxassetid://"..AnimationId
	
	ContentProvider:PreloadAsync({Animation})
	
	local AnimationTrack = Animator:LoadAnimation(Animation)

	if typeof(PlayProperties) == "table" then
		local fadeTime = PlayProperties.fadeTime or 0
		local weight = PlayProperties.weight or 1
		local speed = PlayProperties.speed or 1

		AnimationTrack:Play(fadeTime, weight, speed)
	else
		AnimationTrack:Play()
	end
	
	if recycle then
		local StoppedConnection
		StoppedConnection = AnimationTrack.Stopped:Connect(function()
			Animation:Destroy()
			AnimationTrack:Destroy()
			StoppedConnection:Disconnect()
		end)
	end

	return AnimationTrack
end

I dont think youre allowed to just plop all of your code and ask people to read it all and give advice. You need specific questions or topics regarding your script.