My goal is to write code that accurately grabs the closest UI frame to the mouse position, but with one specific area of the UI it grabs the element which is 1-2 objects away from the actual closest.
My code so far:
local function isNumeric(str)
for i = 1, #str do
local char = str:sub(i, i)
if char < '0' or char > '9' then
return false
end
end
return true
end
local UIS = game:GetService("UserInputService")
local mousePos = UIS:GetMouseLocation()
local closestImageButton = nil
local closestDistance = math.huge
for _, object in v.Parent:GetChildren() do
if object:IsA("ImageButton") and isNumeric(object.Name) then
local imgBtnCenter = object.AbsolutePosition + 0.5 * object.AbsoluteSize
local distance = (mousePos - imgBtnCenter).Magnitude
if distance < closestDistance then
closestImageButton = object
closestDistance = distance
end
end
end
if closestImageButton then
Released = closestImageButton
print(closestImageButton.Name)
end
v.Parent = container of all UI elements
all elements are numbered (1-30)
UI Structure:
Image Label (Parent)
----InputFrame
--------ui list layout
--------1
--------2
--------3
--------4
— 5
—MainSlots
-------UIgridlayout
------- 6
------- 7
--------8
etc
a problem arises when I hover my mouse over the MainSlots Frame,
when I put my mouse over slot 6, it detects the closest as slot13 when
in reality the closest is slot 6.
Any help would be appreciated, thanks!