How do I get the exact mouse position?

I cannot seem to get the correct mouse position for my UI. The whole thing works (if you like an offset mouse).

How can I get the mouse position exactly?

1 Like

To get the mouse you can do:

local mouse = game.Players.LocalPlayer:GetMouse()

For the position you can do:

mouse.X

or

mouse.Y

mouse.x/y will give you your mouse position.

1 Like

Sorry, I meant to provide my script. I have already tried that.

local frame = script.Parent
local infoGui = script.Parent.Parent.Parent.Parent.InfoGui

local userInputService = game:GetService("UserInputService")

local function getMousePosition()
	local mouse = game.Players.LocalPlayer:GetMouse()
	local x, y = mouse.X, mouse.Y
	return x, y
end

local function focus()
	infoGui.Visible = true
	local x, y = getMousePosition()
	infoGui.Position = UDim2.new(0, x, 0, y)
end

local function focusLost()
	infoGui.Visible = false
end

local function move()
	local x, y = getMousePosition()
	infoGui.Position = UDim2.new(0, x, 0, y)
end

frame.MouseEnter:Connect(focus)
frame.MouseLeave:Connect(focusLost)
frame.MouseMoved:Connect(move)
1 Like

I am a bit confused, your title is different to your request, what exactly are you trying to achieve?
Get mouse position or how to position a ui?

2 Likes

I guess I was vague…I am trying to get the exact mouse position as to get the hover gui to be in the proper spot.

1 Like

Ah I see, you mean like the ones on Islands/Bee Swarm?

1 Like

Correct. I have looked around the forum, it seems like this should work…

1 Like

It’s most likely because of the Y position. The Y position is never accurate because of the top-bar, so it will be slightly different on different screens, but you could use this.

print(mouse.y/mouse.ViewSizeY)
print(mouse.x/mouse.ViewSizeX)

This script will get the mouse position accurate on all different sizes of screens, but like I said before, the Y position is slightly different on different sized screens.

3 Likes

I have set “IgnoreGuiInset” to true on all ScreenGuis

1 Like

Then your screen will have an extra 36 pixels, you can try only using the X value (not sure if it would work)

1 Like

You could also extend the GUI above the screen by resizing it in the editor, it works and you can see how it looks in real time.

1 Like

Yeah, or, I could play around with the offset number (x-36 or whatever) right?

3 Likes

Yes, you supposedly could so I guess

1 Like

Just use any method that works.

But like I said, you can get the mouse X and Y like this. I’m pretty sure it works in either local or server script.

local mouse = player:GetMouse()
while wait() do
    print(mouse.y/mouse.ViewSizeY)
    print(mouse.x/mouse.ViewSizeX)
end

While it would be hard and technical to do this, it can be used for things.

if mouse.y/mouse.ViewSizeY > 0.342 then
--code
end

But could you please explain exactly why you want to get the mouse position? Usually doing this is bad practice or there might be another way. For example, I tried getting the mouse position for my game but it was glitchy and hard to make changes to it, but I found a better way to do it.

I suggest using local scripts since its just easy to get the player and all the local stuff @Zompocolypse

1 Like

Bumping this because on every post like it no one seems to know this definitive answer.
UserInputService:GetMouseLocation() is exactly what you’re looking for.

local UserInputService = game:GetService('UserInputService')
local MousePos = UserInputService:GetMouseLocation()
print(MousePos) -- Vector2(x, y)

Not only is it in the format you requested, it also ignores the gui inset that causes your 36 pixel margin of error.

9 Likes