Part moving with mouse issues

  1. What do I want to achieve?
    I need to create a system where by clicking on a part it will follow the players mouse smoothly and when you click again it stops (This is for a trailer)
  2. What is the issue?
    While I have experience with scripting I’m still learning, the issue I’m having is that the part is not following the mouse
  3. What solutions have I tried so far?
    I have looked at some posts and some videos but they all have it so you can move anything and I only need to move certain parts
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = script.Parent

function Clicked()
	part.Position = mouse
end
script.Parent.ClickDetector.MouseClick:Connect(Clicked)

the problem is mouse since its not Vector3 you should do mouse.Hit.p instead of mouse

Doesn’t do anything

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

function Clicked()
	part.Position = mouse.Hit.p
end
script.Parent.ClickDetector.MouseClick:Connect(Clicked)

If you have LocalScript and its placed in StarterPlayerScripts and you should use mouse.Move:function instead

its inside a part, and im using a local script

localscripts doesnt work in workspace

I need it so you can only move certain parts I want

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = script.Parent
local connection

function Clicked()
    if connection then
        connection:Disconnect()
    else
       connection = game.RunService.RenderStepped:Connect(function()
           part.Position = mouse.Hit.p
       end
    end
end

script.Parent.ClickDetector.MouseClick:Connect(Clicked)

@DogeExploder reply’s updated

error on the second end

30303030

still error

303030303030303030

What’s the error? Could u elaborate?

there is no output error its just that the second end is underlined and it doesn’t work

Oops i forgot a bracket:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = script.Parent
local connection

function Clicked()
    if connection then
        connection:Disconnect()
    else
       connection = game.RunService.RenderStepped:Connect(function()
           part.Position = mouse.Hit.p
       end)
    end
end

script.Parent.ClickDetector.MouseClick:Connect(Clicked)

I cannot move the part with my mouse

Umm is your local script in workspace? If so, it won’t run. You have to parent it to StarterPlayerScripts and change part to workspace.PATH_TO_PART

still doesn’t work, the localscript is in starterplayerscripts and I put your code into the script, and I did

local part = workspace.part

To make the part follow the mouse smoothly, you can use a while loop and the DeltaTime property of the RunService to move the part towards the mouse position at a fixed rate.

Here is an example of how you can do this:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local part = script.Parent
local following = false

function Clicked()
	following = not following
end

script.Parent.ClickDetector.MouseClick:Connect(Clicked)

while true do
	wait()
	if following then
		local deltaTime = game:GetService("RunService").DeltaTime
		part:SetPrimaryPartCFrame(part:GetPrimaryPartCFrame():Lerp(mouse.Hit.p, deltaTime * 5))
	end
end

This script will make the part follow the mouse position when you click on it, and stop following when you click again. The Lerp function is used to smoothly interpolate the part’s position towards the mouse position at a rate of 5 units per second.

I hope this helps! Let me know if you have any questions.

where do I put this script?

3030

In a local script you can paste this code :slight_smile:

where should I place the local script is what I was meant?