How do I get the closest GUI to players mouse?

How can I get the closest GUI out of a table of GUI’s to the player mouse’s position?

Ah… What exactly do you want to do here? Would you have the GUI’s already on screen visible to the player?

Yes the GUIs are visible and the TextColor of the closest one to the mouse gets changed from white to yellow. also in a while loop so it’s constantly changing depending on the players mouse

So, even if the player’s mouse is nowhere near any of the GUI’s, still one will always be yellow or will there be a limit?

only one can be yellow at a time. the others stay white except the closest one (sorry for not mentioning that). there will be no limit

No, I mean like, if the mouse would be on the complete opposite side of the screen, would one if the buttons be yellow?

I don’t quite understand what you are trying to say. but if the gui is not the closest one then it gets turned back to white if thats what you mean. as long as its the closest, it will be yellow

This is what I mean:

yes as long as it’s the closest gui

This is the closest I’ve gotten it to work. but for some reason it always makes the last gui yellow (if there are 4, the 4th one is yellow, if there are 8 the 8th etc.)

while menuOpened do
		
		local nearest = nil
		local distanceTable = {}
		for i = 1, #posTable do
			table.insert(distanceTable, #distanceTable+1, (mouse.X - posTable[i].AbsolutePosition.X) + (mouse.Y - posTable[i].AbsolutePosition.Y))
		end
		
		local min = math.min(table.unpack(distanceTable))
		local nearest = posTable[table.find(distanceTable, min)] 
		
		for i = 1, #frame.ItemFolder:GetChildren() do
			frame.ItemFolder[i].TextColor3 = Color3.new(1,1,1)
		end
		
		nearest.TextColor3 = Color3.new(1,1,0)
		
		print(distanceTable)
		print(min)
		print(nearest)
		wait()
	end

What it does here is it adds all of the distances between the guis and the mouse into a table and then it checks for the smallest one (the closest one) and then it associates that to the gui and makes it yellow. it thinks that the last one is the closest

Hm, that’s odd? It looks like that should all work as intended unless I misread something. Well, An easier way would be setting a maximum distance away from the buttons so that it isn’t always targeting a button. Oh and, are you sure they’re all TextButtons and not TextLabels? Because if you have it set to only find TextButtons, it won’t account for as TextLabels.

Yeah I’ve suffered enough. I think I’m just going to stick with TextButtons.

Yeah I’m saying they should all be TextButtons because you have it targeted for those. If the last one is the only one it finds, it’s the only one it can affect.

OK. So basically i was using the wrong formula for distance.
I was using (x2-x1)+(y2-y1).
But the correct formula is √((x2-x1)^2+(y2-y1)^2).

So the working code would be something like this:

while menuOpened do
				
		local distanceArray = {}
		
		for i, v in pairs(posTable) do   --- difference is here below vvvvvvvvvv
			table.insert(distanceArray, i, math.sqrt(math.pow(mouse.X - v.AbsolutePosition.X, 2) + math.pow(mouse.Y - v.AbsolutePosition.Y, 2)))
			print(v.Name, v.AbsolutePosition.X)
		end
		
		local min = math.min(table.unpack(distanceArray))
		local nearest = posTable[table.find(distanceArray, min)] 
		
		for i = 1, #frame.ItemFolder:GetChildren() do
			frame.ItemFolder[i].TextColor3 = Color3.new(1,1,1)
		end

		nearest.TextColor3 = Color3.new(1,1,0)

		print(distanceArray)
		print(min)
		print(nearest)
		wait(.3)
end

les gooo

5 Likes