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:
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.
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
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