Loop causes mass LuaHeap and Signal memory usage

I got a huge smile on my face after finding out the source of my Server Memory leak issue, but I don’t know where to start.

local Runtime = game:GetService("RunService")
wait(2)
while Runtime.Stepped:Wait() do
	for i, v in pairs(game.Workspace.Objects.Plants:GetChildren()) do
		if v.Name == "Dandelion" then
			local connection
			connection = v.ClickPart.ClickDetector.MouseClick:Connect(function(player)
				local Dandelion = game.ReplicatedStorage.Drops.Plants:FindFirstChild("Dandelion Head"):Clone()
				local PlayerName = player.Name
				local Plr = game.Players:FindFirstChild(PlayerName)
				Dandelion.Parent = Plr.Backpack
				local Replace = game.ReplicatedStorage.Replacement.Dandelion:Clone()
				Replace.Position = v.Position - Vector3.new(0, 0.5, 0)
				Replace.Name = "Root"
				Replace.Parent = workspace
				connection:Disconnect()
				v:Destroy()
				v = nil
			end)
		end
		if v:FindFirstChild("ClickDetector") then
			local connection
			connection = v.ClickDetector.MouseClick:Connect(function()
				if v.Name == "Log" then
					game.ReplicatedStorage.Loot.Logs.Value = game.ReplicatedStorage.Loot.Logs.Value + 1
					v:Destroy()
					v = nil
				elseif v.Name == "Stick" then
					game.ReplicatedStorage.Loot.Sticks.Value = game.ReplicatedStorage.Loot.Sticks.Value + 1		
					v:Destroy()
					v = nil
				elseif v.Name == "Stone" then
					game.ReplicatedStorage.Loot.Stones.Value = game.ReplicatedStorage.Loot.Stones.Value + 1		
					v:Destroy()
					v = nil	
				elseif v.Name == "Raspberry" then
					local connection2
					connection2 = v.ClickDetector.MouseClick:Connect(function(player)
						local Rasp = game.ReplicatedStorage.Drops.Plants:FindFirstChild("Raspberry"):Clone()
						local PlayerName = player.Name
						local Plr = game.Players:FindFirstChild(PlayerName)
						local Rand = math.random(1,3)
						if Rand == 3 then
							local Char = workspace:FindFirstChild(PlayerName)
							Char.Humanoid:TakeDamage(5)
							local Physical = Char.Humanoid:GetAttribute("PhysicalDMG")
							Char.Humanoid:SetAttribute("PhysicalDMG", Physical + 5)
						end
						Rasp.Parent = Plr.Backpack
						connection2:Disconnect()
						v:Destroy()
						v = nil
						connection2 = nil
					end)
				end
				connection:Disconnect()	
				connection = nil
			end)
		end
	end
end

I assume it has something to do with the I, v in pairs() and the function inside, but I’m not sure how to go about fixing that. I’m not looking for someone to edit the script for me but rather someone who can type up a quick explanation and solution to this.

Thanks!

I think looping through all the items and connecting events, especially if you have a lot of items, will cause a lot of memory usage. Multiple connections take up a lot of memory.

Why do you even need to loop through all the items a bunch of times?

You’re performing a lot of operations every frame.

I thought having too many scripts is worse but I’m a rookie to this memory stuff, should I just get rid of this script and add a script that checks for a mouse click in every object that needs one?

You just need to loop through once and connect the events. You don’t need to constantly loop through all the objects.

Your essentially doing

v.Clicked:Connect(func)
v.Clicked:Connect(func)
v.Clicked:Connect(func)
v.Clicked:Connect(func)
v.Clicked:Connect(func)
v.Clicked:Connect(func)
v.Clicked:Connect(func)
v.Clicked:Connect(func)
-- so on and so on
1 Like