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

is there any way to get the mouse position? if so then how?

Asked by
seikkatsu 110
5 years ago

Pretty self explanatory. I would like to get the mouse position. Any help will be apriciated thanks in advance

1 answer

Log in to vote
2
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You can do that by getting the mouse's Hit.p property. In the code below hitPosition is the 3D position in the workspace that the mouse is hitting. hitPosition is a vector3 value in the workspace.

Make sure you put this in a localscript

mouse = game.Players.LocalPlayer:GetMouse()
hitPosition = mouse.Hit.p
print(hitPosition)

Here's an example that moves a brick to the position of the mouse: We need to put the part in the mouse's target filter otherwise the mouse will try to put the brick at the position of the part over and over again and i'll look glitchy.

--create a new part
part = Instance.new("Part", game.Workspace)
part.Size = Vector3.new(1,1,1)
part.Anchored = true

mouse = game.Players.LocalPlayer:GetMouse()
mouse.TargetFilter = part -- add the part to the target filter

--get current hitposition update position of part to be hitPosition
while(wait())do
    local hitPosition = mouse.Hit.p
    part.Position = (hitPosition)
    print("Position: ",hitPosition)
end



Ad

Answer this question