Button event firing twice or more

With the following…

code snippet
local VirtProgramTrigger_Program
VirtProgramTrigger_Program = VirtProgramTriggerButton.MouseButton1Click:Connect(function()
	for _, btn in pairs(ScannerButtons) do
		if btn:IsA("ImageButton") then
			btn.MouseButton1Click:Connect(function()
				if btn.Name == "Scanner1" and btn.Enabled.Value == true then
					print(Scanner1)
					if not Scanner1 then
						Scanner1 = true
						btn.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0)
					else
						Scanner1 = false
						btn.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0)
					end
				elseif btn.Name == "Scanner2" and btn.Enabled.Value == true then
					if not Scanner2 then
						Scanner2 = true
						btn.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0)
					else
						Scanner2 = false
						btn.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0)
					end
				elseif btn.Name == "Scanner3" and btn.Enabled.Value == true then
					if not Scanner3 then
						Scanner3 = true
						btn.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0)
					else
						Scanner3 = false
						btn.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0)
					end
				elseif btn.Name == "Scanner4" and btn.Enabled.Value == true then
					if not Scanner4 then
						Scanner4 = true
						btn.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0)
					else
						Scanner4 = false
						btn.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0)
					end
				end
			end)
		end
	end

… I am trying to make a button red or green, depending on debouncing. However, the event fires twice and bugs the whole thing out. I put a print to debug what the value is, and it’s definitely something wrong.
Video of demonstration:

What should I do?

2 Likes

Where’s the code snippet located, and, is it a localscript or a regular script?

The problem is that each time the button is clicked, it keeps adding more and more connections to the clicking. You need to disconnect the old functions each time you are creating new ones.

2 Likes

LocalScript under StarterGui (UIs)

Should I disconnect the NextButton connection? Disconnecting the first, original one (RunProgram) disallows me to use the UI button at all.

I didn’t mean for you to disconnect that one, but the ones you add with the for loop. I would make a table, add the connections, and disconnect them each time the button is clicked.

local VirtProgramTrigger_Program
local conns = {} --create the table to store the connections.
VirtProgramTrigger_Program = VirtProgramTriggerButton.MouseButton1Click:Connect(function()
	for i,conn in pairs(conns) do
		conn:Disconnect() --disconnect the click connection.
	end
	for _, btn in pairs(ScannerButtons) do
		if btn:IsA("ImageButton") then
			conns[#conns + 1] = btn.MouseButton1Click:Connect(function() --put the connection in the table
				if btn.Name == "Scanner1" and btn.Enabled.Value == true then
					print(Scanner1)
					if not Scanner1 then
						Scanner1 = true
						btn.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0)
					else
						Scanner1 = false
						btn.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0)
					end
				elseif btn.Name == "Scanner2" and btn.Enabled.Value == true then
					if not Scanner2 then
						Scanner2 = true
						btn.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0)
					else
						Scanner2 = false
						btn.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0)
					end
				elseif btn.Name == "Scanner3" and btn.Enabled.Value == true then
					if not Scanner3 then
						Scanner3 = true
						btn.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0)
					else
						Scanner3 = false
						btn.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0)
					end
				elseif btn.Name == "Scanner4" and btn.Enabled.Value == true then
					if not Scanner4 then
						Scanner4 = true
						btn.Frame.BackgroundColor3 = Color3.fromRGB(0,255,0)
					else
						Scanner4 = false
						btn.Frame.BackgroundColor3 = Color3.fromRGB(255,0,0)
					end
				end
			end)
		end
	end
4 Likes

On this line of code…

btn[#BtnConnections + 1]

I get the error:

LocalScript:127: invalid argument #2 (string expected, got number)

Can I see the whole script? I think you changed some variable names.

Also, right here, I think you meant BtnConnections not btn as “btn” is the button itself and is expecting a string to get a child of the button.

BtnConnections[#BtnConnections + 1]
1 Like
local BtnConnections = {}
VirtProgramTrigger_Program = VirtProgramTriggerButton.MouseButton1Click:Connect(function()
	for _, btn in pairs(ScannerButtons) do
		if btn:IsA("ImageButton") then
			btn[#BtnConnections + 1] = btn.MouseButton1Click:Connect(function()
				if btn.Name == "Scanner1" and btn.Enabled.Value == true then
					print(Scanner1)
					-- continuing stuff

Yea, it is what I pointed out here.

Right, thanks! That fixed my problem!