Is this a memory leak?

I made a service that creates 3 events for each response. It’s basically just a dialogue service like the ones you see in fisch or grow a garden.

I’m wondering if this is a memory leak or not.

for responseNum, response in dialogueInfo.Responses do
		local responseClone = responseTemplate:Clone()

		responseClone.Number.Text = "#"..responseNum
		responseClone.Button.MainText.Text = response
		responseClone.Parent = self.playerOptions.Content
		
-- These functions are what I'm worrying about 
		responseClone.Button.MouseEnter:Connect(function()
			tweeningModule.Tween(responseClone.Button.MainText, {Position = responseClone.Button.MainText:GetAttribute("TweenPos")}, .1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0, false)
			tweeningModule.Tween(responseClone.Button, {ImageTransparency = responseClone.Button:GetAttribute("HoverTransparency")}, .25, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0, false)
		end)

		responseClone.Button.MouseLeave:Connect(function()
			tweeningModule.Tween(responseClone.Button.MainText, {Position = responseClone.Button.MainText:GetAttribute("OgPos")}, .1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0, false)
			tweeningModule.Tween(responseClone.Button, {ImageTransparency = responseClone.Button:GetAttribute("NormalTransparency")}, .25, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, false, 0, false)
		end)

		responseClone.Button.MouseButton1Click:Connect(function()
			self.playerOptions:Destroy()
			self:playerReply(response, true)
			self.response:Fire(dialogueNum, responseNum)
		end)
	end

These probably aren’t an issue. I don’t really understand how this code loops but just make sure that you don’t have overlapping connections at the same time. Here’s what I mean:

Bad:

while true do
	RunService.Heartbeat:Connect()
	
	task.wait(1)
	
	-- Code continues without disconnecting the previous connection
end

Good:

RunService.Heartbeat:Connect()
-- Connection only created once, no need to disconnect

Good (alternative):

while true do
	local connection = RunService.Heartbeat:Connect()
	
	task.wait(1)
	connection:Disconnect()
	-- Code continues but connection is disconnected
end

Looking at your code, it seems like as long as you are :Destroy()ing the children of self.playerOptions after the response is clicked, you should be fine. Roblox automatically closes connections tied to destroyed objects.

Oh okay. I was worried that the connections would still be playing in the background :sweat_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.