Disconnect function connection

Hello there, I’ve been trying to find a way to disonnect the connection in this function below and I cant figure out how. Is it necessary to do that here? Any ideas how? Should I just have a button inside the template instead with a script inside it(although I appreciate the tidy look of having one script)?

Context: the below function is used to create buttons inside a scrolling frame that the player can click to buy items from. Template is just an image inside the script with a Text Label inside it.

local function addToFrame(item, parent)
	local scrollingFrame = script.Parent:WaitForChild(parent):WaitForChild("ScrollingFrame")

	local newTemplate = template:Clone()
	newTemplate.Name = item.Name
	newTemplate.ItemName.Text = item.Name
	newTemplate.Parent = scrollingFrame

	buttonconnections[#buttonconnections+1] = newTemplate.MouseButton1Click:Connect(function()
		local success = false
		success = game.ReplicatedStorage.CheckSale:InvokeServer(item.Name)
		if success then
			newTemplate:Destroy()
		end
	end)
end

Thanks for your time!!!

Do you need to cache each event connection in a table?

It would be a simpler approach to just construct the new event listener object directly. It will clean up automatically when the button is destroyed, so you don’t have to worry about that.

1 Like

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