Function Firing Multiple Times

When I click all the check marks in the video below after clicking apply and then click continue then cancel and repeat this, there are no issues. However, when I click apply and then cancel without clicking any check marks, the function duplicates. Everytime I do this, it duplicates again and again. Any ideas?

function Questions(Application)
    for _, Question in QuestionsGui:GetChildren() do
        if Question.Name == Application.Name then
            Question.Visible = true

            for _, Check in Question.PageOne:GetDescendants() do
				if Check:IsA("ImageButton") then
					Check.MouseButton1Click:Once(function()
						ClickSound:Play()
						Check.ImageColor3 = Color3.fromRGB(70, 104, 68)
						CheckCounter += 1
						
						if CheckCounter == 5 then
							Question.PageOne.Continue.Visible = true
						end
					end)
				end
			end
         end
     end
end

I think I see the issue.

When Questions() is run, 5 connections using :Once() are made to each check button. When the buttons are pressed, the connection goes away, HOWEVER when you close the form using Cancel, the connections you made using :Once() remain.

This means that when you reopen the form, Questions() is called again, making 10 total connections using :Once(), two connections for each check button… When you click the checks the second time around, checkCounter increases by 2 because there are two connections firing.

To fix this, you need to destroy the connections when cancel is clicked, OR you need to only create the connections once, using :Connect().

The first option would look something like this:


local connectionArray = {}
function Questions(Application)
    for _, Question in QuestionsGui:GetChildren() do
        if Question.Name == Application.Name then
            Question.Visible = true

            for _, Check in Question.PageOne:GetDescendants() do
				if Check:IsA("ImageButton") then
					local onceConnection = Check.MouseButton1Click:Once(function()
						ClickSound:Play()
						Check.ImageColor3 = Color3.fromRGB(70, 104, 68)
						CheckCounter += 1
						
						if CheckCounter == 5 then
							Question.PageOne.Continue.Visible = true
						end
					end)
                    table.insert(connectionArray, onceConnection)

				end
			end
         end
     end

-- run this when cancel is pressed
local function deleteConnections()
    for i, j in pairs(connectionArray) do
        j:Disconnect()
    end
end

The second would be a fundamental change to your script. You’d have to make a new function that runs once when the script starts that creates the connections to the buttons using :Connect(). When apply is pressed, you would need to make the buttons interactable (using imageButton.interactable) and change their colors to be that gray color, and when they’re pressed you would need to make them uninteractable (using imageButton.interactable = false) if that makes sense.

I made this in the reply box so I can’t testify to its syntax, but hopefully you can implement the concept.

1 Like

It looks like the buttons are stacking, make sure to store the connections and disconnect them when the player leaves the page.
The person before me explained it.

1 Like

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