As title states, I’m trying to sort through GUIs and find the one with the closest on-screen position to the players mouse.
1 Like
(Frame.AbsolutePosition - Frame2.AbsolutePosition).Magnitude
gives the distance between 2 GUIs. That’s all you need to know
1 Like
My way is probably not the best way to go about it but, you can through each gui object, and check if the mouse x and mouse z position.
If you want it to be corner adjusting then thats slightly harder, but since you just want the closest one thats not a problem.
Here is my take on it.
PlayerGui
LocalScript
local playerGui = script.Parent -- In this case my script is just placed inside the PlayerGui
local mouse = game:GetService('Players').LocalPlayer:GetMouse()
local function getClosestGui(origin,maxdist,cycle)
local dist = maxdist
local target = nil
for _, guiObj in pairs(cycle) do -- Cycle is a table of all the gui parts we want to check
local objdist = (origin - guiObj.AbsolutePosition).Magnitude -- Magnitude
if objdist < dist then -- Change the dist if its under the old dist
dist = objdist
target = guiObj
end
end
return target -- Returns the gui object
end
mouse.Move:Connect(function()
local objects = {}
for _, v in pairs(playerGui.ScreenGui:GetChildren()) do
-- Filtering out all of the non interface objects
if v:IsA('Frame') or v:IsA('TextLabel') or v:IsA('TextButton') or v:IsA('TextBox') or v:IsA('ScrollingFrame') or v:IsA('ImageLabel') or v:IsA('ImageButton') then
table.insert(objects,v)
end
end
local target = getClosestGui(
Vector2.new(mouse.X,mouse.Y), -- 2d origin
math.huge, -- Max distance
objects -- List of gui objects
)
print(target) -- Boom we got our target
end)
1 Like
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.