Possible Memory Leak Issues

I was seeing a lot of games around Thanos and the “snap.” So I challenged myself to make my own. I have everything working when I noticed that it was running slowly. So, I changed some stuff that might have caused it and, the same result. I turned on performance stats and saw the memory seemed really high. I though this might be the problem. Here is the place file: SnapTest.rbxl (27.2 KB) I don’t know if it is something obvious, but either way! :man_shrugging:

From what I can tell, you’re using pcall to encapsulate your function. pcall doesn’t start a new thread, so currently it has to loop over each part one by one, which will take 100 cycles per part. Put a spawn/coroutine around it or use tweens instead.

Try this for your “Disintegrate” module:

local tweenService = game:GetService("TweenService")
local serverStorage = game:GetService("ServerStorage")
local debris = game:GetService("Debris")

-- Variables
local ash = serverStorage:WaitForChild("Ash")

local random = Random.new(os.time())
local tweenInfo = TweenInfo.new(3, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

-- Private Functions
local function startDisintegration(part)
	local ashClone = ash:Clone()
	ashClone.Color = ColorSequence.new(part.Color, Color3.new(25, 15, 0))
	ashClone.Parent = part
	
	debris:AddItem(ashClone, 10)
	
	delay(random:NextNumber(0, 7), function()
		local tween = tweenService:Create(part, tweenInfo, { Transparency = 1 })
		tween:Play()
	end)
end

-- Public Functions
local Disintegrate = {}
function Disintegrate:dis(player)
	local character = player.Character
	
	if character then
		for _, child in pairs(character:GetChildren()) do
			if child:IsA("BasePart") and child.Name ~= "HumanoidRootPart" then
				startDisintegration(child)
			end
		end
	end
end

return Disintegrate
2 Likes

Ok, this worked! It was not a memory problem. I did have a spawn in there originally but I think a couple of changes I made messed it up. Either way, your solution worked! :smile:

1 Like

No problem! I included a bit of randomization in my code example too if you’re interested.