Unsure if I should report this as a bug (would, but too lazy to make a repro atm) but as far as I’m aware this has always happened, and it’s incredibly infuriating, and not how I believe MouseEnter should work.
I have a bunch of buttons, each with MouseEnter events connected to them. However, notable flaws are that they still work even if a frame is above them, causing unwanted problems
Any possible solutions?? I don’t want to do
if not frame.Visible then
As I use the MouseEnter function is a variety of different scripts for just the 1 case of buttons here. Example being, 1 script handles the hover sound effect, another handles the hover frame info.
I wrote you some code. You’ll need to change it to fit your application though. Here’s how it works:
We keep track of all ‘depths’ (how deep a frame is nested)
We keep track of all hovered over frame
We select the highest hovered frame
When we leave it, we try select the next highest hovered frame (if there’s any left)
local topFrame, topFrameDepth = nil, 0
local hovered = {}
local cached = {}
local function getFrameDepth(f)
if not cached[f] then
local depth = 0
local parent = f
repeat
depth = depth + 1
parent = parent.Parent
until parent:IsA("ScreenGui") or not parent
cached[f] = depth
end
return cached[f]
end
local function updateTopFrame()
-- Sort hovered frames by depth in descending order
table.sort(hovered, function(a, b)
return getFrameDepth(a) > getFrameDepth(b)
end)
-- Set the new top frame
if #hovered > 0 then
local newTopFrame = hovered[1]
local newTopFrameDepth = getFrameDepth(newTopFrame)
if newTopFrame ~= topFrame then
if topFrame then
topFrame.BackgroundColor3 = Color3.new(1, 1, 1)
end
topFrame = newTopFrame
topFrameDepth = newTopFrameDepth
topFrame.BackgroundColor3 = Color3.new(0.5, 0.5, 0.5)
end
else
if topFrame then
topFrame.BackgroundColor3 = Color3.new(1, 1, 1)
end
topFrame = nil
topFrameDepth = 0
end
end
for _, v in ipairs(script.Parent:GetDescendants()) do
if v:IsA("Frame") then
v.MouseEnter:Connect(function()
if not table.find(hovered, v) then
table.insert(hovered, v)
end
updateTopFrame()
end)
v.MouseLeave:Connect(function()
local index = table.find(hovered, v)
if index then
table.remove(hovered, index)
end
updateTopFrame()
end)
end
end