How can I Hide UI/GUI after some seconds

What do you want to achieve?
I want that UI/GUI to auto-hide after some seconds but i’m still not that good at UI/GUI scripting so this is making me stay confused…

What solutions have you tried so far?
Devforum, but i didn’t find anything useful for what i need…

Code:

local players = game:GetService('Players')
local RS = game:GetService('RunService')

local plr = players.LocalPlayer
local gui = script.Parent
local guiAreas = workspace:WaitForChild('SinisTest')


local param = OverlapParams.new()
param.FilterType = Enum.RaycastFilterType.Whitelist

RS.RenderStepped:Connect(function()
	local char = plr.Character
	if not char then return end
	param.FilterDescendantsInstances = {char.PrimaryPart}
	
	for _, area in ipairs(guiAreas:GetChildren()) do
		local overlap = workspace:GetPartsInPart(area, param)
		if #overlap > 0 then
			gui[area.Name].Visible = true
		else
			gui[area.Name].Visible = false
		end
	end
end)

Assuming gui is a ScreenGui, you can simply turn the Enabled property on or off to make the gui appear and vice versa. Hope that helps you figure out what to do!

But that isn’t the issue that i’m having… Visible already does what i want it to do, i just want the interface to hide after for example 5 seconds or so.

I think I understand now. Since you run this every frame, you can probably create a system using os.clock(). Something like…

local usedTime = os.clock()
local maximumHoldTime = 5

RS.RenderStepped:Connect(function()
	if os.clock() >= usedTime + maximumHoldTime then
		-- it's been 5 seconds!
	end
end)

Hope this helps!

Are you talking about;

local GUI = game:GetService("Players).LocalPlayer:WaitForChild("ScreenGui")
GUI.Visible = true
task.wait(1)
GUI.Visible = false

or if the UI or GUI is not being used like no longer in focus? You could use Button/GUI.MouseLeave and use tick and when it reaches a certain value then you hide the UI/GUI. UserInputService could also work, I think there’s something there that you could use.

(https://create.roblox.com/docs/reference/engine/classes/UserInputService)

Best I could come up on a whim.

I could use that, but not sure on how to implement that into the code i wrote…

To make sure, you want the auto-hiding feature to apply to gui[area.Name], correct?

Yup, to apply to gui[area.Name] since i have more areas each with their respective Frame and TextLabel.

if #overlap > 0 then
	gui[area.Name].Visible = true
	task.spawn(function()
		local cancel = false
		for count = 0, 50, 1 do
			task.wait(0.1)
			if gui[area.Name].Visible = false then cancel = true break end
		end
		if cancel == true then return end
		gui[area.Name].Visible = false
	end)
else
	gui[area.Name].Visible = false
end

Made in a hurry, but I hope it helps. You essentially just need to wait 5 seconds and make sure the gui hasn’t been turned off in that time. There are probably better ways to do this though…

That didn’t work… but don’t worry i’ll try to find a fix to that issue.

I just found a solution myself, so i could say that this is solved!

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