How to hide UI after inactivity?

Hi, it’s been a while since I posted, sorry about that.

I was wondering if anyone knows how to automatically hide a SurfaceGui (or really Ui in general) after 3 seconds have passed since it was activated (I’m using a function to toggle visiblity of the UI).

It’s basically a volume bar for a TV I’m making. Here’s what I tried doing:

local RecentChange = tick()

--Some code later...

task.spawn(function()
 task.wait(3)
 if math.round(RecentChange) > 3 then
  GUI.Enabled = false
 end
end)

However, this didn’t work. If anyone has an idea, or knows how to do it themselves, please let me know! Thank you :slight_smile:

task.delay provides the coroutine that it schedules to execute after the time passes. You can close and recreate it whenever the UI is activated.

  • Keep a thread variable outside whatever function handles a volume change
  • Inside the volume change function:
    • task.close the thread variable if it isn’t nil
    • Re-set the thread variable to a task.delay call with a function that hides the UI

EDIT: accidentally used “coroutine” instead of “task” for task library functions, fixed.

1 Like

I’m not sure I understand. Can you give me an example?

local uiCloserThread: coroutine;

local function onUIActivated()
	ui.Visible = true
	
	-- do stuff
	
	if uiCloserThread then
		task.close(uiCloserThread)
	end
	uiCloserThread = task.delay(3, function()
		ui.Visible = false
	end)
end