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)
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
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)
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)
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)
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)
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.