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

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


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 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 ="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

1 Like