Answered by
5 years ago Edited 5 years ago
You can use the :GetGuiObjectsAtPosition() method of PlayerGui
. If we apply this in conjunction with the .Move signal of Mouse
, we can perpetually track the X & Y position of the Cursor on-screen. By feeding these two properties into our method of PlayerGui, we can find what GuiObjects
the Cursor is currently hovering over.
Since :GetGuiObjectsAtPosition()
returns an array, we use a pairs loop to iterate through each Instance. and filter for a GuiButton
.
01 | local Player = game:GetService( "Players" ).LocalPlayer |
03 | local PlayerGui = Player:WaitForChild( "PlayerGui" ) |
04 | local Cursor = Player:GetMouse() |
06 | local function ChangeCursorOverGui() |
07 | local X,Y = Cursor.X, Cursor.Y |
08 | local GuiObjects = PlayerGui:GetGuiObjectsAtPosition(X,Y) |
09 | for _,Button in pairs (GuiObjects) do |
10 | if (Button:IsA( "GuiButton" )) then |
17 | Cursor.Move:Connect(ChangeCursorOverGui) |
Use this code within a LocalScript under StarterGui
for appropriate placement. Some debouncing such as an IsAlreadyHovering
boolean would increase efficiency, though isn't much of an issue to worry about.
I have tested this in Studio, and it works marvelously!
If this helped, don't forget to accept this answer!