How to temporary stop a function

the code

local playfunc = playbtn.MouseButton1Click:Connect(function()
play.btnVisible = false
wait(10)
options.Visible = true
end)
exitplay.MouseButton1Click:Connect(function()

menu.Visible = false
playfunc:Disconnect()
end)

Hello i am trying to make a button temporary stop a function.
so basically i want that if the player clicks the playbtn, it waits for 10 secs and it makes the option visible but if the player clicked the exit play, it shouldn’t wait for 10 secs and make the option visible or anything in that function. i tried using Disconnect() but it diconnects it forever.

2 Likes

Store the function in a variable, and connect it to the playfunc.MouseButton1Click event when you want to undisconnect.

local myFunc = function()
play.btnVisible = false
wait(10)
options.Visible = true
end

local playfunc = playbtn.MouseButton1Click:Connect(myFunc)

exitplay.MouseButton1Click:Connect(function()
menu.Visible = false
playfunc:Disconnect()
end)

to undisconnect it, just do this:

playfunc = playbtn.MouseButton1Click:Connect(myFunc)

make sure to NOT add the local when you are just undisconnecting it. only use local when creating the variable, AKA when you are connecting it for the first time in the script.

1 Like

thanks, i just tried it and doesn’t seem to work. am i supposed to disconnect the myfunc instead of playfunc?

local canPlayfunc = true

local playfunc = playbtn.MouseButton1Click:Connect(function()
	if not canPlayfunc then
		return
	end
	play.btnVisible = false
	wait(10)
	options.Visible = true
end)

exitplay.MouseButton1Click:Connect(function()
	menu.Visible = false
	canPlayfunc = false
end)

:sweat_smile:
why not just

local playfunc = playbtn.MouseButton1Click:Connect(function()
play.btnVisible = false
wait(10)
if menu.Visible == false then return end
options.Visible = true
end)

or

local debounce = false
local playfunc = playbtn.MouseButton1Click:Connect(function()
if debounce then return end
debounce = true
play.btnVisible = false
local count = 0
while menu.Visible do
	if count >= 10 then
		options.Visible = true
		break
	end
	count += 1
	task.wait(1)
end
debounce = false
end)

or
using disconnect and undisconnect method

after i click the exit button, it still waits for 10 secs and makes options to be visible.

I didn’t add that wait(10), you did. Just remove it if you don’t want it.

i know. i dont think you understand what i want, i wanted to make it that if the the exit is clicked, it doesn’t wait for 10 secs and make options visible. Kinda want it to stop the function temporary

local canPlayfunc = true

local playfunc = playbtn.MouseButton1Click:Connect(function()
	if not canPlayfunc then
		return
	end
	play.btnVisible = false
	options.Visible = true
end)

exitplay.MouseButton1Click:Connect(function()
	menu.Visible = false
	canPlayfunc = false
end)

So just get rid of wait(10) like I suggested.

1 Like

well, i need it to be there, the code i sent isn’t the one im using, its just an example. the real code im using really needs the wait to be there

play.btnVisible = false should be playbtn.Visible = false

I just noticed after finally checking the code.

1 Like
local playfunc = playbtn.MouseButton1Click:Connect(function()
	playbtn.Visible = false
	task.wait(10)
	options.Visible = true
end)

exitplay.MouseButton1Click:Connect(function()
	menu.Visible = false
	playfunc:Disconnect()
end)
1 Like

i already fixed that myself lol, the only problem is what i said

If you want the exit button to make an options section/page visible then simply add “options.Visible = true” to the code. You’ll need the variable named “options” to refer to the correct UI element. Could you provide a screenshot of how everything is organised in StarterGui?

Does this work?

Just filling space to make a legal post…

1 Like

You can make use of coroutines which can be yielded and resumed whenever you want it to:

local stop = false

local coro = coroutine.create(function()
    -- Code here
    if stop then
        coro.yield() -- Pauses the function
    end
end)

coro.resume() -- Resumes the function

oops, or this. I have no idea what you are doing…