Know whatever GUI the Mouse is hovering

Is there a way to know correctly whatever the mouse is hovering in GUI?
I tryed to do this script

local Plr = game.Players.LocalPlayer
local GuiE = GetAllChildren(Player.PlayerGui) --Custom function that give an array of all childrens (more that :GetChildren(), this function is working very well!
local GUIET = nil
local T={}
local i=0
for i=1,#GuiE do --Mouse detector!
      table.insert(T,i,false)
      pcall(function() --It can be a Folder or something that isn't a Gui object
            GuiE[i].MouseEnter:Connect(function()
                  T[i]=true
            end)
            GuiE[i].MouseLeave:Connect(function()
                  T[i]=false
            end)
      end)
end
while wait() do --It changes GUIET to return what the mouse is hovering
      for i=1,#T do
            GUIET=nil
            if T[i] then
                  GUIET=GuiE[i]
            end
      end
end

This work but the Script is too heavy and it uses a lot of performances (crucial for slow devices) and it don’t work sometimes (when two objects are in the same spot) AND it’s even unstable (from why the usage of a pcall) :frowning:
I’m looking for a way to get correctly the Gui Object but in less lines of code :slightly_smiling_face: (like the Target propertie of Mouse)

9 Likes

Visit this wiki page to see how a function of StarterGui works. It will detect whatever GUI element is at a given position. Use Mouse.Position so you can give the position value as the parameters of the function.

Mouse.Move:Connect(function()
    local GUIs = StarterGui:GetGuiObjectsAtPosition(Mouse.Position.X, Mouse.Position.Y-36)
end)
17 Likes

Theres GetGuiObjectsAtPosition for PlayerGui which might help. You can call this every frame.

Btw, the position you pass to this function already takes the top bar into account. If you’re passing the position of the mouse on-screen, then you’ll need to subtract that by the height of the topbar. GetGUIInset will help with that.

Besides that, you’re doing pcalls unnecessarily. You can check if something is of a certain baseclass with :IsA(classname).

1 Like

I do pcall because it’s faster (to code) than doing something that could know if it’s not a Gui object…

It’s not faster at all. All gui objects inherit from GuiObject. You can check if it’s a gui object with blah:IsA("GuiObject")

So… With that, I take the position and the size of ALL Gui Objects then find where the mouse is in?
Using the mouse’s coordinates
I also like to evoluate my comments :slight_smile: By adding text

GetGuiObjectsAtPosition takes an X and a Y. Not a UDim2.

1 Like

It can be UDim (1) but not in this case… It’s Vector2, so using X and Y is right :slight_smile:

I updated the code to use X and Y coordinates, so it should work by now… I think?

I didn’t saw that it return objects! I thought it return Vector2!

It works :slight_smile: And the operation method is very usefull since it returns all Gui Objects found
I answer in multi-posts, so… Sometimes I can answer from the fourth or else post

GetGuiObjectsAtPosition will return a table of the objects at a given position, if you were to access these objects, you must use

for i,v in ipairs(GUIs) do
    --code
end

I was allways trying to understand, WHAT IS ipairs() ? :thinking:
I never needed to use it :slight_smile: I think

ipairs() is used for indexing a table with number indexes, unlike pairs() is supposed to be used in dictionaries, but could also be used in regular tables. It just makes code analysing easier.

Correct
local Dictionary = {
    ["Iteration 1"] = "Value",
    ["Iteration 2"] = "Value"
}

for i,v in pairs(Dictionary) do
    print(i,v)
end

Output:

Iteration 1 Value
Iteration 2 Value

local Table = {Value,Value}

for i,v in ipairs(Table) do
    print(i,v)
end

Output:

1 Value
2 Value

Incorrect
local Dictionary = {
    ["Iteration 1"] = "Value",
    ["Iteration 2"] = "Value"
}

for i,v in ipairs(Dictionary) do
    print(i,v)
end
1 Like

… :neutral_face:
I will find myself in the “Wiki”
:confused: I don’t understand:

Returns three values: an iterator function, and the table t .

It’s too much “sumarized”

Ok, with you I understand better! :smile: Why the Wiki is hard to understand on ipairs() ?

To let you know pcall just ignores errors.

I know, I used because it was fast to code :slight_smile:

What do you mean “fast to code”?

Like really fast to type?

For this, I mean fast to type! :smile:

It’s your choice but I will suggest you to don’t code like that.