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)
I’m looking for a way to get correctly the Gui Object but in less lines of code (like the Target propertie of Mouse)
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)
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).
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 By adding text
It works 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
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