I don't understand why this function is causing a memory leak

I made an inventory system where every time you click “equip” it fires this function on the server to update your character’s items. I’m not sure why this is causing the game to lag immensely. I feel like I just need a second pair of eyes because when I look at it I really don’t see how it could cause such an immense amount of lag…

function Refresh(player, Humanoid, Character)
	Humanoid:RemoveAccessories()
	wait()
	local Equipment = player.EquipmentFolder
	local Hat = game.ReplicatedStorage.Equipment:FindFirstChild(Equipment.Hat.Value)
	if Hat and Hat ~= ("None") then
		local HatCloned = Hat:Clone()
		Humanoid:AddAccessory(HatCloned)
	end
	local Wand = game.ReplicatedStorage.Equipment:FindFirstChild(Equipment.Wand.Value)
	if Wand and Wand ~= ("None") then
		local WandCloned = Wand:Clone()
		Humanoid:AddAccessory(WandCloned)
		playAnimationFromServer(Character, 6472863718)
	end
	local Accessory = game.ReplicatedStorage.Equipment:FindFirstChild(Equipment.Accessory.Value)
	if Accessory and Accessory ~= ("None") then
		local AccessoryCloned = Accessory:Clone()
		Humanoid:AddAccessory(AccessoryCloned)
	end
	local Hair = game.ReplicatedStorage.Equipment:FindFirstChild(Equipment.Hair.Value)
	if Hair and Hair ~= ("None") and Hair ~= ("Hair01") then
		local HairCloned = Hair:Clone()
		Humanoid:AddAccessory(HairCloned)
	end
	local Robe = game.ReplicatedStorage.Equipment:FindFirstChild(Equipment.Robe.Value)
	if Robe and Robe ~= ("None") then
		if Character:FindFirstChild("Shirt") then
			Character:FindFirstChild("Shirt"):Destroy()
		end
		if Character:FindFirstChild("Pants") then
			Character:FindFirstChild("Pants"):Destroy()
		end
		local Shirt = Robe.Shirt:Clone()
		local Pants = Robe.Pants:Clone()
		Shirt.Parent = Humanoid.Parent
		Pants.Parent = Humanoid.Parent
	end
end

Please help if you have any suggestions at all. Thank you :slight_smile:

Couple ideas… Do you have appropriate debounce in place? Add a print to this function and make sure you aren’t calling it many times for each click. A second thought, what is the typical change on equip? Does just one item change at a time or do you pick a whole new set and got equip? If it is one at a time, you would be better off looking at what is different and only removing that one item instead of destroying it all and making new clones of the same things.

Just added a debounce in place and it didn’t help too much. The way it works is it refreshes the entire character at once. I’ll try changing it to an update type system instead and see what happens

How sure are you that this is a memory leak?
Additionally, how sure are you that the memory leak is from this specific part of this specific script?

I’m very sure. When I comment out the refresh function it does not lag my game. When i add it back in my memory usage goes out of control

What calls the refresh function? Is it possible it could be a problem not with the refresh function itself but how or where its used?