Need help with stopping a function from Connect(function() inside it

Is it possible to achieve the stopping of a function from connected function inside it with lua? If so, what can be put on the third line to make the a() function stop? I’ve been looking for a solution for a long time, but haven’t been able to find it

function a()
game.Players.LocalPlayer.PlayerGui.SkipButton.TextButton.MouseButton1Click:Connect(function()
--- straightaway stop function a()
end)  
-- stuff that will happen inside the function that can be stopped by pressing a button
end

a()

Maybe something like this ? i dont really understood what you were asking but i think it work, if you need help ill help u faster : lobox920#9889

Without specifics of what you are trying to achieve no. Lua makes the connection and immediately continues functioning, the connected function will not run until a() is finished or yields. a() will not stop and nothing can interrupt it, only a() can give up this control by calling yielding functions.

local Script = script
local TextButton = Script.Parent
local Routine

local function f()
	local Thread = coroutine.running()
	local Connection
	local function OnClick()
		coroutine.close(Thread)
		Connection:Disconnect()
	end
	Connection = TextButton.MouseButton1Click:Connect(OnClick)
	
	for _ = 1, 20 do
		task.wait(1)
		print("Hello world!") --Printing stops once the button is clicked.
	end
end

Routine = coroutine.create(f)
local Success = coroutine.resume(Routine)
5 Likes

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