Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I cause a Part to follow the cursor?

I made an attempt to accomplish this:

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

while true do
	local bob = game.Workspace.Bob
	bob.Position = Vector3.new(mouse.Hit)
end

I read that you need to use Mouse.Hit for it's position but I am probably wrong. What's the proper way to get the position of the mouse?

1 answer

✓ Accepted answer

mouse.Hit will return the mouse's CFrame, you need to add a .p to mouse.Hit to get the position like so, mouse.Hit.p, or you could just change bob's CFrame, instead of Position.

local plr = game.Players.LocalPlayer
local m = plr:GetMouse()

game:GetService("RunService").RenderStepped:Connect(function()
	local bob = workspace.Bob
	if bob then
		bob.CFrame = m.Hit
	end
end)

6 comments

0

Thanks, and the RenderStepped is a good idea. I didn't think of that at the time.

0

Yeah man. If I helped you, please accept this as an answer <3

0

It didn't work. I tried switching from CFrame = m.Hit to Position = m.Hit.p and same result

0

It works for me...

0

Use GetService("Mouse").Move instead of RenderStepped.

0

Nevermind, it works. My studio was just bugging out.

This discussion is preserved as it was. New answers, comments, and votes are closed.