More Efficient Way of Checking Mouse.Target?

I am making a script where if you’re hovering over certain parts in Workspace then it’ll make a GUI visible and then invisible when you stop hovering. But right now I’m constantly checking using while wait() do and there’s got to be a better, non-laggy, way to do it. Does anyone know what it is?

Edit: For future reference; I’ve marked @robloxgamerzys reply as the solution but this might not apply to everyone using it, if you need a different answer read through the other replies to this topic.

cant you just use a clickDetector? It has functions for mouse hover enter and mouse hover leave
https://developer.roblox.com/en-us/api-reference/class/ClickDetector

Actually yes, this would work, I already have click detectors inside of the items anyway so I can just use that function.

Ye, you dont need to click it, when the mouse hovers over it, it will fire

ClickDetector.MouseHoverEnter:Connect(function()
	print("enter")
end)

You could use Userinputservice.InputChanged with the downside of the gui sometimes wont update if you walk without moving the mouse. Also, instead of making a ton of loops, you could just check if there is a mouse.target and then update the popup if the target is a child of products within the input changed event. If you wanted to, you could also use runservice to make sure it always updates.

You can use the mouse.Move event instead of a while loop or, and idk if this works: mouse:GetPropertyChangedSignal("Target"):Connect(function()

local Game = game
local RunService = Game:GetService("RunService")
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local Target = Instance.new("Part") --Hack.

local function OnRenderStep()
	if not Mouse.Target or (Mouse.Target == Target) then return end
	print("Mouse's target changed from "..Target.Name.." to "..Mouse.Target.Name..".")
	Target = Mouse.Target
end

RunService.RenderStepped:Connect(OnRenderStep)
1 Like