Why are multiple frames being cloned when checking multiple buttons at once?

local ProximityPrompt = script.Parent
local Screen = workspace.Screen

ProximityPrompt.Triggered:Connect(function(Player)

	
	local MainScreen = Player.PlayerGui:FindFirstChild("MainScreen")
	MainScreen.Adornee = Screen
	

	local Buttons = MainScreen.Main.Scanned.Items.ScrollingFrame:GetChildren()
	local SelectedOrder = MainScreen.Main.Scanned.SelectedOrder

	for _, v in pairs(Buttons) do
		if v:IsA("Frame") then
			local connection = v.AddTo.MouseButton1Click:Connect(function()
				local Item = SelectedOrder.Template:Clone()
				Item.Parent = SelectedOrder.ScrollingFrame
			end)
		end
	end
end)

Every time the proximity prompt is triggered the Item is cloned the amount of times its been triggered. But if I use :Once() then I wont be able to click it again. Is there a better way to prevent this cloning or check through a list of buttons?

well I am not sure what you mean by the text below, but what you asked in the title, could be because

for _, v in pairs(Buttons) do
		if v:IsA("Frame") then
			local connection = v.AddTo.MouseButton1Click:Connect(function()
				local Item = SelectedOrder.Template:Clone()
				Item.Parent = SelectedOrder.ScrollingFrame

you are looping through all the buttons and then cloning the item as many times as there are buttons that are a frame.

It’s not cloning the number of buttons as I have about 10 frames, it is cloning once the first time i trigger the prompt, then twice the second time im triggering the prompt and so on.
(I am clearing the frame each time)

local ProximityPrompt = script.Parent
local Screen = workspace.Screen

-- Table to store connections
local connections = {}

ProximityPrompt.Triggered:Connect(function(Player)
	local MainScreen = Player.PlayerGui:FindFirstChild("MainScreen")
	MainScreen.Adornee = Screen
	
	local Buttons = MainScreen.Main.Scanned.Items.ScrollingFrame:GetChildren()
	local SelectedOrder = MainScreen.Main.Scanned.SelectedOrder

	-- Disconnect previous connections
	for _, connection in pairs(connections) do
		connection:Disconnect()
	end

	-- Clear the connections table
	connections = {}

	for _, v in pairs(Buttons) do
		if v:IsA("Frame") then
			-- Store the new connection in the connections table
			table.insert(connections, v.AddTo.MouseButton1Click:Connect(function()
				local Item = SelectedOrder.Template:Clone()
				Item.Parent = SelectedOrder.ScrollingFrame
			end))
		end
	end
end)

Thank you I appreciate your help!

1 Like

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