Need helping hiding gui after mouse leaves

I already know someone is about to say “Use clickdetectors”

i want to hide the gui when a players mouse leaves the part i made it so it shows the gui but need help on when the mouse leaves the part to hide the gui

local camera = workspace.CurrentCamera
local uis = game:GetService("UserInputService")
local LocalPlayer = game.Players.LocalPlayer

local function mouseRaycast()
	local mousePos = uis:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePos.X, mousePos.Y)

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {LocalPlayer.Character}
	params.FilterType = Enum.RaycastFilterType.Exclude

	local result = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 50, params)
	return result
end

local HoverOverGuis = game.Workspace.HoverOverGuis

game:GetService("RunService").RenderStepped:Connect(function()
	local raycast = mouseRaycast()

	if raycast then
		if raycast.Instance:HasTag("HoverGui") then
			raycast.Instance.MainGui.Enabled = true
			
			if not raycast.Instance:HasTag("HoverGui") then
				for _, i in pairs(HoverOverGuis:GetChildren()) do
					if i:IsA("BillboardGui") then
						i.Enabled = false
					end
				end
			end
		end
	end
end)
1 Like

Why can’t you use click detector ?

1 Like

because they suck i figured it out though so ima close this post

if you want to know how i fixed it i made a for loop that looks through workspace:GetDescendants()) and looks for the tag “HoverGui” and if it isnt tagged then its added to the table and ignored by the raycast to avoid errors like Main Gui is not a valid member of Baseplate then i used a for loop to search through a folder for the parts with the guis and disables the guis that are enabled after the mouse leaves

local function mouseRaycast()
	local mousePos = uis:GetMouseLocation()
	local mouseRay = camera:ViewportPointToRay(mousePos.X, mousePos.Y)
	
	local nonHoverparts = {}
	
	for _, part in pairs(workspace:GetDescendants()) do
		if not part:HasTag("HoverGui") then
			table.insert(nonHoverparts, part)
		end
	end

	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {LocalPlayer.Character}
	params.FilterType = Enum.RaycastFilterType.Exclude

	local result = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 25, params) --change 25 to the max distance
	return result
end

local HoverOverGuis = game.Workspace.HoverOverGuis

game:GetService("RunService").RenderStepped:Connect(function()
	local raycast = mouseRaycast()

	if raycast then
		if raycast.Instance:HasTag("HoverGui") then
			raycast.Instance.MainGui.Enabled = true
		else
			raycast.Instance.MainGui.Enabled = false
		end
	else
		for _, gui in pairs(HoverOverGuis:GetChildren()) do
			gui.MainGui.Enabled = false
		end
	end
end)
2 Likes

Understandable, have a great day.

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