How do I know what GUI the mouse is hovering currently?

Is there any way I can know what GUI a player is hovering his mouse at?
Basically, I’m trying to make an inventory system, and right now I’m trying to make it so you can switch item slots.

1 Like

You could use :MouseOver event that comes with all GUI objects (More Info).

What you likely want to do is use game.Players.LocalPlayer:GetMouse(), which returns the player mouse object. You can then get the .X and .Y of the mouse. Using .Button1Down you can detect when the user presses their mouse down. With the above, you now know where the user is pressing. To move items, simply check if the mouse if over a object using .AbsolutePosition and .AbsoluteSize. For example:

local m = game.Players.LocalPlayer:GetMouse()

local function isHoveringOverObj(obj)
    local tx = obj.AbsolutePosition.X
    local ty = obj.AbsolutePosition.Y
    local bx = tx + obj.AbsoluteSize.X
    local by = ty + obj.AbsoluteSize.Y
    if m.X >= tx and m.Y >= ty and m.X <= bx and m.Y <= by then
        return true
   end
end

local object = --Object to check
m.Button1Down:Connect(function()
    if isHoveringOverObj(object) then
        print('Object Pressed')
    end
end)

Now, with a little modification, you can make this into a inventory system. (I have in the past)

If you have questions on a specific part, feel free to ask. However, #scripting-support is not for free scripts, and you will have to put some effort in to get the finished product.

You could use the MouseEnter event on every GUI object.

local currentGui

guiObject.MouseEnter:Connect(function()
     currentGui = guiObject
end)

guiObject2.MouseEnter:Connect(function()
     currentGui = guiObject2
end)

--etc.

Use :GetGuiObjectsAtPosition()

I don’t need an answer anymore, but thanks for helping. I used something different for swapping slots in the inventory, and it’s working fine.