Get the Distance Between the Players Mouse and a UI Button

Title says it all, is there a way for me to see the distance between the players mouse position and the position of a GUI?

What I have tried: Since Mouse.Position does not exist I tried doing Mouse.X + Mouse.Y but then I do not know how to go about finding it from the gui.

Please read this before posting a question.

Fixed the format, sorry about that, I forgot to add what I have tried.

I believe you can do something like this

local mouse = game.Players.LocalPlayer:GetMouse()
local runService = game:GetService("RunService") -- get runservice before the loop


local function getPos()
    while true do
        print(yourGui.Position-mouse.Position) -- Change "yourGui" with your gui
    	runService.RenderStepped:Wait()
    end
end

Ah ok, ill try that. Thanks, I’ll let you know if it works.

Oh wait, Like I said In the Post, position is not a valid property of mouse, its only mouse.x and mouse.y

Fixed:

local mouse = game.Players.LocalPlayer:GetMouse()
local runService = game:GetService("RunService") -- get runservice before the loop


local function getPos()
	while true do
		print(script.Parent.Position-UDim2.new(0,mouse.X, 0,mouse.Y)) -- Change "yourGui" with your gui
		runService.RenderStepped:Wait()
	end
end

getPos()

This returns a UDim2 value

Actually did mistake or i can’t be right (If you using GUi mouse position and not world mouse position then allright). You need mouse.Hit:

local mouse = game.Players.LocalPlayer:GetMouse()
local runService = game:GetService("RunService") -- get runservice before the loop


local function getPos()
	while true do
		print(script.Parent.Position-UDim2.new(0,mouse.Hit.p.X, 0,mouse.Hit.p.Y)) -- Change "yourGui" with your gui
		runService.RenderStepped:Wait()
	end
end

getPos()
2 Likes

Thank you, I will try this and get back to you.

Yes, you are totally right. My mistake :slight_smile:

1 Like