How do I get the mouse position on the screen?

basically I want to be able to get the position of the mouse on the screen.
Now the mouse that’s you know touching parts or anything.

I need like the vector2 position of it.

If you have any questions feel free to ask

2 Likes

Can’t you just do…

Vector2.new(mouse.X, mouse.Y)

The mouse will of course be the player’s mouse.

3 Likes

This does not work. Considering it is taking the X position of the mouse in the game. And not with a vector2. There for if I were to move it on the Z axis it would do nothing.

Again, this line is difficult to decipher:

“There for if I were to move it on the Z axis it would do nothing”

The mouse has no Z axis, a ray cast from the mouse would have a Z axis but you ask for screen coordinates which are inherently X and Y. To get those then just put this code in a LocalScript (I named mine Mouse) in starter GUI with a path like this:

image

local UserInputService = game:GetService("UserInputService");
local RunService = game:GetService("RunService");
local Text = script.Parent.ScreenGui.TextLabel;
RunService.RenderStepped:Connect(function()
	
	local mouseLocation = UserInputService:GetMouseLocation();
	Text.Text = mouseLocation.X .. " " .. mouseLocation.Y;

end)
2 Likes

It’s difficult to decipher what you mean. The first line makes sense but the second line:

“Now the mouse that’s you know touching parts or anything”

is hard to figure what you are saying here!

By that I mean the vector2. Like the mouse on the screen.
Also I tried what you said. When I go into first person it stops working

Because in first person the mouse is locked to screen centre, so the onscreen position will always be the coordinates that are the middle of the screen, regardless of which direction the player is looking.

2 Likes

Oh yeah! Thanks that makes since!
How do I make it unlock the mouse in first person?

add a textbutton on your screengui and enable the modal

You can simulate first person by moving the camera into the head of the player and then you get full movement capabilities without screen lock of the mouse. There may be better methods but I know not of them, I’m sure someone else might know?

Um. what does it mean to enable the 'MODAL" I dont know what that is

I thought that too, it may be a question of changing the behaviour enum:

local UserInputService = game:GetService("UserInputService")
UserInputService.MouseBehavior = Enum.MouseBehavior.Default

You may have to ascertain when in first person mode, maybe there is a camera Enum that will tell you this?

its property of textbutton go to property tab and scroll down and tick the “modal”