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

How do I make a part follow my mouse (PC and Mobile)?

I want to make it wo where a player summons a part and it follows where their mouse is but I'm not sure how to do that.

1 comment

0

Have you tried altering the CFrame/Position of the part to the CFrame/Position of the mouse, probably using an offset based off of the distance to the position of the mouse?

1 answer

✓ Accepted answer

once the player summons the part, call this function that places the part on the mouse position:

local player= game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local character = player.Character or player.CharacterAdded:Wait()
local onRenderedStep 

--function that makes the part follow the mouse
function makePartFollowMouse(part)
	if(not mouse.TargetFilter)then
		mouse.TargetFilter = part
	end 

	part.CFrame = 
			CFrame.new(mouse.Hit.p) --place brick at mouse's position 
			* CFrame.new(0, part.Size.Y/2, 0)--shift up brick so the brick on top of the mouse's position instead of smack dab in the middle
end

part = Instance.new("Part", workspace)
onRenderedStep = game:GetService("RunService").RenderStepped:Connect(function() makePartFollowMouse(part) end)

--if you want to stop makign the brick follow the mouse do the following:
--onRenderedStep:Disconnect()

2 comments

0

Is this in a local script or normal?

0

since we're using renderstepped, it should be a localscript. you can make it a server script by replacing the renderstepped function with a while loop or by using remote events

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