Button.activated firing multiple times even with debounce

It’s firing multiple times but I don’t want to and I don’t want it to fire multiple times

I’m not sure if that’s what you want, but here is the script for now(not finished)

local Storage = game:GetService("ReplicatedStorage")

local screen = workspace.Ecrans.Ecran.SurfaceGui
local line = Storage.Gui.Ligne
local spawnLineEvent = Storage.Events.SpawnLineEvent

spawnLineEvent.OnServerEvent:Connect(function(player, destination, vol, porte)
	print(player.Name.."created a flight with this parameters :"..destination, vol, porte)
	local newLine = line:Clone()
	newLine.Destination.Text = destination
	newLine.Vol.Text = vol
	newLine.Porte.Text = porte
end)

I mean where you are calling, so the part of your code where you call spawnLineEvent:FireServer(). what you’re showing is what happens when the event was received.

Here is the code where i’m calling the function

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 activatedConnection --For disconnection reference

local lock = true

local function CheckTextBox(TextBoxes, Gui)
	--local lock = true --/!!/  Moved above the function so lock isn't always true


	--/!!/  IF you need this function more than once then you'll need to make logic for lock to be set to true again

	for value, index in pairs(TextBoxes:GetChildren()) do --/!!/  You could switch this for loop around instead of setting lock false, sets it to true
		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
		--/!!/  If lock is true then we need to set it false
		
		lock = false --/!!/  If you only want this code block to run once 
		
		print("Button is showed to",player)
		Gui.Creer.Visible = true
		
		activatedConnection = 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)
			
			activatedConnection:Disconnect()  --/!!/  After everything above disconnect this connection
			
		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)

The script didn’t worked and the button wouldn’t show without modifications

After multiple tries, I figured out how to solve the problem : I have put the Gui.Creer.Activated inside the

command.Triggered:Connect(function()
	if not player.PlayerGui:FindFirstChild("NewFlight") then
		OpenGUI()
		if #player.PlayerGui.NewFlight:GetDescendants() == #Storage.Gui.NewFlight:GetDescendants() then 
			print("Loaded")
			local Gui = player.PlayerGui.NewFlight
			local textBoxes = Gui.Page.TextBox
				Gui.Page.Creer.Activated:Once(function() -- Run if the button is clicked
				print("Activated")
				local destination = textBoxes.Heure.Text.."  "..textBoxes.Destination.Text
				local vol = "TX      "..textBoxes.Vol.Text
				local porte = textBoxes.Porte.Text
				spawnLineEvent:FireServer(destination, vol, porte)
				return true
			end)
		end
	end
end)

Thanks you for help.

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