Button.activated firing multiple times even with debounce

I have this script :

if lock == true then
		print("Button is showed to",player)
		Gui.Creer.Visible = true
		Gui.Creer.Activated:Connect(function()
			local destination = TextBoxes.Heure.Text.."  "..TextBoxes.Destination.Text
			local vol = "TX      "..TextBoxes.Vol.Text
			local porte = TextBoxes.Porte.Text
			spawnLineEvent:FireServer(destination, vol, porte)
		end)

When I click on the Button on the Gui (Gui.Creer), it fires multiple times, event with a debouce variable, it don’t do nothing, I already thinked of solutions but I want to know is there is a way to make the function fire only once only in this part of code, if you need the entire code, I can send it, thanks for answers.

1 Like

Please show me more of the script so i can better analyse your problem.

local Players = game:GetService("Players")
local Chat = game:GetService("TextChatService")
local Storage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer

local command = Chat:WaitForChild("TextChatCommands"):WaitForChild("NewFlight")
local newFlightGUI = Storage:WaitForChild("Gui"):WaitForChild("NewFlight")
local spawnLineEvent = Storage.Events.SpawnLineEvent

local function CheckTextBox(TextBoxes, Gui)
	local lock = true
	
	for value, index in pairs(TextBoxes:GetChildren()) do
		if index:IsA("TextBox") then
			if index.Text == index.Name or index.Text == "" and index.Name ~= "Heure" then
   				lock = false
			elseif index.Name == "Heure" then
				if index.Text == "Heure (xx:xx)" or index.Text == "" then
					lock = false
				end
			end
		end	
	end
	
	if lock == true then
		print("Button is showed to",player)
		Gui.Creer.Visible = true
		Gui.Creer.Activated:Connect(function()
			local destination = TextBoxes.Heure.Text.."  "..TextBoxes.Destination.Text
			local vol = "TX      "..TextBoxes.Vol.Text
			local porte = TextBoxes.Porte.Text
			spawnLineEvent:FireServer(destination, vol, porte)
		end)
		
	else
			Gui.Creer.Visible = false	
	end
end

local function OpenGUI()
	print("Triggered")
	newFlightGUI:Clone().Parent = player.PlayerGui
	
	local gui = player.PlayerGui:WaitForChild("NewFlight"):WaitForChild("Page")
	local textBoxes = gui:WaitForChild("TextBox")
	
	gui.Creer.Visible = false
	for _, index in pairs(textBoxes:GetChildren()) do
		if index:IsA("TextBox") then
			local oldText = index.Text
			
			index.Focused:Connect(function()
				index.ClearTextOnFocus = false
				CheckTextBox(textBoxes, gui)
			end)
			
			index.FocusLost:Connect(function()
				index.ClearTextOnFocus = false
				CheckTextBox(textBoxes, gui)
			end)
			
			index:GetPropertyChangedSignal("Text"):Connect(function()
					index.ClearTextOnFocus = false
					CheckTextBox(textBoxes, gui)
			end)
		end
	end
end

command.Triggered:Connect(function()
	if not player.PlayerGui:FindFirstChild("NewFlight") then
		OpenGUI()
	end
end)

Here the script

Perhaps it’s because lock is local to the function and not the whole script.

No, because lock is only used to show or hide the button.

Same what @TestyLike3 said its probably because the debounce is in the function so when you use the function lock is immediately true. Try putting it outside the function

By the way, you got the index and value variables in the for loops the wrong way round. Index is first, value is second. Besides, you can rename them however you want, so you can do for _, textBox in pairs(textBoxes:GetChildren()) do

I tried it, but nothing changes

Now i really cant think of anything to solve your problem, but try replacing pairs with ipairs.

1 Like

Can we see how you’ve changed it, just in case you somehow made an error?

Replacing it with ipairs doesn’t change anything.

The lock variable or the ipairs ?

Debounces typically always take place outside of any functions to avoid spamming the button even when the conditions are not met. I suggest you simply add this to your code, not changing anything else.

local debounce = false
local DEBOUNCE_TIME = 2

local function OpenGUI()
	if not debounce then
		debounce = true --//Lock the function

		... --//Run script

		task.wait(DEBOUNCE_TIME)
		debounce = false --//Unlock the function after the given wait time
	else --//If debounce is active..
		print("Waiting for debounce.")
	end
end

I’ve put the script in the OpenGUI function, but it changes nothing, the RemoteEvent is send multiple times.

Have you tried recreating it in another experience? (do not duplicate the .rbxl)

The problem is that Button.Activated is being called within a function, but the Button.Activated function never gets disconnected. This means that every time you call the CheckTextBox() function, you are creating a new Activated connection. This has nothing to do with debounce and rather that you are connecting multiple Activated connections which means over time you’ll get more and more repeated calls.

What you need to do is define Activated as a variable outside of its own scope so that you can disconnect it once you’re finished with it. Try modifying this part of your code to this:

if lock == true then
	print("Button is showed to",player)
	Gui.Creer.Visible = true

    local listedToActivation --Create a variable outside its scope
	listedToActivation = Gui.Creer.Activated:Connect(function() --Define the variable to the Activated function
        listedToActivation:Disconnect() --Immediately disconnect the function after its first Activated listen event.

		local destination = TextBoxes.Heure.Text.."  "..TextBoxes.Destination.Text
		local vol = "TX      "..TextBoxes.Vol.Text
		local porte = TextBoxes.Porte.Text
		spawnLineEvent:FireServer(destination, vol, porte)
	end)
		
else
		Gui.Creer.Visible = false	
end

I’ve put this script in the CheckTextBox() function and deleted the before Activated trigger, but sadly, it’s the same, I don’t know if I did something wrong while replacing the previous script, but it continued to firing multiple time

Does it fire multiple times initially? You’re saying this happens when you press the “Creer” button but I don’t see that function anywhere in your provided code. I am suspecting that whatever debounce you’re using is not working properly or you are somehow repeatedly calling the CheckTextBox() function. Could you post the code snippet of the Creer button function?

When I press the button, the function spawnNewLine:FireServer() fires multiple times, the debounce thing I made is not in the code I sent because it didn’t worked, so I deleted it

Here, everything is in the script I posted

Well then isn’t that the problem? You’re firing spawnNewLine:FireServer() multiple times which is creating multiple Activated connections resulting in multiple activation calls. Why are you calling spawnNewLine:FireServer() multiple times? Can you post that code snippet where this RemoteEvent is being called?