Question about mouse functions

Hey devs, how do I lock a part to the player’s mouse position?

You could use move the Mouse.Move event, which fires each time the Mouse moves (Creative name 10/10)

Since the Mouse can only be accessed by the client, you should do this in a LocalScript:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Part = --Part you want to define here

Mouse.Move:Connect(function()
    Part.Position = Mouse.Hit.Position --Moving the part to where the mouse's position is
end)
1 Like

ahhh thanks a trillion mate, happy developing :smiley:

One more question, what if I wanted to do this with a model instead of a part?

You could do

model.PrimaryPart.Position = Mouse.Hit.Position

By moving the PrimaryPart, the whole model should move. You have to make sure you assign the Primary part of the model, otherwise this would error.

1 Like

well now that only moves the primary part, not necessarily the whole model
also, it has this weird zoom glitch, I’ll post a screen capture.

My bad, I must have gotten it wrong. You could move it via CFrame, by:

:SetPrimaryPartCFrame(CFrame.new(Mouse.Hit.Position))

You can also call :MoveTo() on the model, something like:

Model:MoveTo(Mouse.Hit.Position)

Tried that, but it performs worse than the previous method. Also, is there a way to fix that bug that i sent in the video?

You tried both methods I suggested?

That bug in the video can be triggered by a couple of different things. Are you setting the camera’s CFrame to a part? If so, the part may be interfereing with the mouse (an easy way to test this would be to see if it occurs with just a normal part). It could also be happening because the model is not anchored. Try anchoring the model.

Yup, I set the CFrame of the camera to a part. Is there any resolution?

Also, yeah, the MoveTo() function performs worse than the :SetPrimaryPartCFrame function, so I went with that.

The reason of this is because the mouse touches the part and collides with the part instead of going through it to the floor. You should use Raycasting instead and use FilterDescendantInstances.

Thanks a million! I’ll try that.

You can also fix that issue by doing this:

Mouse.TargetFilter = --insert model/part thing here

Assuming TargetFilter excludes a part from interfering with mouse functions?

I don’t really understand what you’re saying (english isn’t my first language), but when I had the same issue, I did TargetFilter instead and it worked fine.

1 Like

Mouse.TargetFilter can be used to filter an object & its descendants

Supposedly in the video you uploaded, if you set this property to ignore a part, the mouse’s position would ignore it & go through the next possible position

Mouse.TargetFilter = workspace.RandomPartsToFilterOut