Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I get the position of a player's mouse cursor?

Asked by 3 years ago

I am making a thing in which the player's mouse hovers over something and I want a small GUI to go where the mouse is displaying some information. It works 100%, but I need to get the player's mouse position. Thx!

1
You can use UserInputService for a cleaner position.                                                        local mousePos = game:GetService("UserInputService"):GetMouseLocation() killerbrenden 1537 — 3y

2 answers

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

There's 2 possible answers. One preferred over the other.

*Option 1 is using UserInputService.

Option 2 is using the Player's Mouse.*

UserInputService

local UIS = game:GetService("UserInputService")
local mousePos = UIS:GetMouseLocation()

This will return the position in offsetted pixels, not the location pointed in workspace.

Player's Mouse

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

local mouseX = mouse.X
local mouseY = mouse.Y

--// This will return the position in offsetted pixels, not the location pointed in workspace.

local mousePosW = mouse.Hit.p or mouse.Hit --//  Vector3 - CFrame
--// The CFrame of the mouse’s position in 3D space

local mousePosWW = mouse.Origin.p or mouse.Origin --// Vector3 - CFrame
--//A CFrame positioned at the Workspace.CurrentCamera and oriented toward the Mouse's 3D position.

--// This will return the position in workspace, not the location in offsetted pixels.

Hope this helped! Feel free to select this as an answer if this helped you!

I recommend doing more research on the Player's Mouse, very useful!

Ad
Log in to vote
1
Answered by 3 years ago
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Move:Connect(function()
wait()
print(Mouse.X) 
print(Mouse.Y)
end)

You can change the mouse.Move to whenever you want to get the players mouse position, and the print was just an example to show you where the players mouse is.

0
Thanks! R_LabradorRetriever 198 — 3y

Answer this question