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?
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?
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.
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)
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?
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.
Ah I see, you mean like the ones on Islands/Bee Swarm?
Correct. I have looked around the forum, it seems like this should work…
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.
I have set “IgnoreGuiInset” to true on all ScreenGuis
Then your screen will have an extra 36 pixels, you can try only using the X value (not sure if it would work)
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.
Yeah, or, I could play around with the offset number (x-36 or whatever) right?
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
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.