Check for mouse Position in UI

Hi, I was working on this system that checks if the player mouse is on a UI frame. I wanted to use the player mouse, get the X and Y position and check if those are ontop of the frame. But now it does nothing :sweat_smile:

This is my code:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
	local Blocks = script.Parent:GetChildren()
	
	for _, B in pairs(Blocks) do
		if B:IsA("Script") then
			table.remove(Blocks, B)
		else
			local X = Mouse.X
			local Y = Mouse.Y
			
			for _, B2 in pairs(Blocks) do
				local B2SizeX = B2.Size.X
				local B2SizeY = B2.Size.Y
				local B2PositionX = B2.Position.X
				local B2PositionY = B2.Position.Y
				
				local MaxX = B2PositionX + B2SizeX
				local MaxY = B2PositionY + B2SizeY
				local MinX = B2PositionX
				local MinY = B2PositionY
				
				if X >= MinX or X <= MaxX and Y >= MinY or Y <= MaxY then
					print("Mouse is on top of "..B2.Name)
				end 
			end
		end
	end
end)

Does someone maybe know how this works?

1 Like

Use AbsolutePosition and AbsoluteSize for this.

1 Like

There are a few ways you can do this.

  1. Make an inputBegan connection for every block ui to detect mouse movement.
  2. Calculate the distance between mouse.X, mouse.Y and block absoluteposition.X and absoluteposition.Y.
  3. Use :GetGuiObjectsAtPosition(mouse.X, mouse.Y), check if the array is empty. If itโ€™s not empty, the mouse is on top of the frame.
1 Like

I replied to the wrong person, but I wrote down the possible solutions ^^

1 Like

It workt, I choose the 3e one:

TYSM

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.