Having trouble to disconnect an function

I have this anonymous function that I have put inside a variable to disconnect it

local triggered

triggered = command.Triggered:Connect(function(triggeringPlayer, flight)
	if not player.PlayerGui:FindFirstChild("NewFlight") then
		if flight:match("%d+") then
			local match = flight:match("%d+")
			print(flight:match("%d+"))
			for value, index in pairs(screen) do
				if index:IsA("Frame") and index.Vol.Text:match(tostring(match)) then
					print(index.Vol.Text:match(tostring(match)))
				else
					local wrongFlightGui = Storage.Gui.WrongFlight:Clone()
					wrongFlightGui.Parent = player.PlayerGui
					print("Started")
					wait(1.5)
					wrongFlightGui:Destroy()
					triggered:Disconnect()
				end
			end
		end
		OpenGUI()

		if #player.PlayerGui.NewFlight:GetDescendants() == #Storage.Gui.NewFlight:GetDescendants() then 
			local Gui = player.PlayerGui.NewFlight.Page
			local textBoxes = Gui.TextBox
			local previous = nil
			if flight:match("%d+") then
				previous = flight:match("%d+")
				print(flight:match("%d+"))
			end

			Gui.Creer.Activated:Once(function()

				local destination = textBoxes.Destination.Text
				local heure = textBoxes.Heure.Text
				local vol = "TX   "..textBoxes.Vol.Text
				local porte = textBoxes.Porte.Text
				local statut = Gui.Statut.Text
				local couleur = Gui.Statut.TextColor3

				spawnLineEvent:FireServer(heure, destination, vol, porte, statut, couleur, previous)

				local line = workspace.Ecrans.Ecran.SurfaceGui.Ecran

				line.ChildAdded:Once(function(line)

					if #line:GetChildren() == 6 then

						if line.Destination.Text == destination and line.Heure.Text == heure and line.Porte.Text == porte and line.Statut.Text == statut and line.Vol.Text == vol then
							Gui.Message.Text = "La ligne a été créée."
							Gui.Message.BackgroundColor3 = Color3.fromRGB(53, 255, 30)
							Gui.Message.Visible = true
							wait(1.5)
							Gui.Parent:Destroy()
						else 
							Gui.Message.Text = "Il y a eu une erreur dans la création de la ligne !"
							Gui.Message.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
							Gui.Message.Visible = true
							wait(1.5)
							Gui.Parent:Destroy()
						end
					end
				end)
			end)
		end
	end
end)

But in the :Disconnect() function, this message is there :
image
I tried many things, but I’ve came to a conclusion that when I use command:Triggered, I can’t disconnect it even with a variable, is it a bug or do somebody how to resolve the bug without creating a full function, or just stopping the function where the :Disconnect() is ?
Thanks for any help

triggered = command.Triggered:Connect(function(triggeringPlayer, flight)
	if not player.PlayerGui:FindFirstChild("NewFlight") then
		if flight:match("%d+") then
			local match = flight:match("%d+")
			print(flight:match("%d+"))
			for value, index in pairs(screen) do
				if index:IsA("Frame") and index.Vol.Text:match(tostring(match)) then
					print(index.Vol.Text:match(tostring(match)))
				else
					local wrongFlightGui = Storage.Gui.WrongFlight:Clone()
					wrongFlightGui.Parent = player.PlayerGui
					print("Started")
					wait(1.5)
					wrongFlightGui:Destroy()
					return -- Disconnect can only be called outside of the function
				end
			end
		end
		OpenGUI()

		if #player.PlayerGui.NewFlight:GetDescendants() == #Storage.Gui.NewFlight:GetDescendants() then 
			local Gui = player.PlayerGui.NewFlight.Page
			local textBoxes = Gui.TextBox
			local previous = nil
			if flight:match("%d+") then
				previous = flight:match("%d+")
				print(flight:match("%d+"))
			end

			Gui.Creer.Activated:Once(function()

				local destination = textBoxes.Destination.Text
				local heure = textBoxes.Heure.Text
				local vol = "TX   "..textBoxes.Vol.Text
				local porte = textBoxes.Porte.Text
				local statut = Gui.Statut.Text
				local couleur = Gui.Statut.TextColor3

				spawnLineEvent:FireServer(heure, destination, vol, porte, statut, couleur, previous)

				local line = workspace.Ecrans.Ecran.SurfaceGui.Ecran

				line.ChildAdded:Once(function(line)

					if #line:GetChildren() == 6 then

						if line.Destination.Text == destination and line.Heure.Text == heure and line.Porte.Text == porte and line.Statut.Text == statut and line.Vol.Text == vol then
							Gui.Message.Text = "La ligne a été créée."
							Gui.Message.BackgroundColor3 = Color3.fromRGB(53, 255, 30)
							Gui.Message.Visible = true
							wait(1.5)
							Gui.Parent:Destroy()
						else 
							Gui.Message.Text = "Il y a eu une erreur dans la création de la ligne !"
							Gui.Message.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
							Gui.Message.Visible = true
							wait(1.5)
							Gui.Parent:Destroy()
						end
					end
				end)
			end)
		end
	end
end)

-- out here you can disconnect it

It worked, thank you so much, normaly, :Disconnect() can be used inside the function, I’ve tried wih another function and it worked, but I don’t know if it’s normal, but with command.Triggered, it doesn’t work, and I thought return wasn’t possible to use inside an anonymous function, but thanks you.

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