Making a part follow player smoothly

I have created a script that makes part ("Pet1) Follow humanoid root part but the issue I am having are two things:
One: how can I make it look a lot smoother (maybe I’m following the wrong part of the character or maybe I should use a different technique
Two: How can I make the part stay parallel to player (so rotations still work) :white_check_mark:

Here is what happens so far:

-- 
local Humanoid = script.Parent.HumanoidRootPart
local distance = 5
local Pet1 = workspace.Pets.Pet1
local Follower = Humanoid

while true do
	Pet1.CFrame = Follower.CFrame*CFrame.new(0, 0, distance)
	Pet1.CFrame = CFrame.new(Pet1.CFrame.X, 1, Pet1.CFrame.Z)
	wait()
end

Yes you can weld the parts together but I would always like the part to be sitting on the ground, I have gone with the method of keeping all the ground one level so I can always set it to y=1 but if there is another better method please say

EDIT:
Working smoothly just trying to get it to rotate

1 Like

You should try tweening it (if possible)

I thought about that but I’m wanting to try to keep it a constant distance away so I’m not sure how well that will work with tweening

Lerping? Also, about second question you need to try CFrame.

CFrame contains Rotation and Position information, so it will be parallel to humanoidrootpart if you type:

Part.CFrame = HumanoidRootPart.CFrame * Offset
-- Lerp Example
for i=0,1,0.01 do -- or 0.02
   Part.CFrame = Part.CFrame:Lerp(HumanoidRootPart.CFrame * Offset,i)
end

You may change offset to make it behind character, not inside a humanoidrootpart.

1 Like

Use a body position for that pet to follow you, no tween is required

Thanks for all the options, I’m just going through them trying different things, I will say more ASAP

Its the best method to make such things.

Thanks for the advice to use Heartbeat, it’s making it move perfectly smooth now I’m just doing the rotation now (the code I use for that was):

local Humanoid = script.Parent.HumanoidRootPart

local distance = 5

local Pet1 = workspace.Pets.Pet1
local Follower = Humanoid

local RunService = game:GetService("RunService")
while true do
	RunService.Heartbeat:Connect(function()
		Pet1.CFrame = Follower.CFrame*CFrame.new(0, 0, distance)
		Pet1.CFrame = CFrame.new(Pet1.CFrame.X, 1, Pet1.CFrame.Z)
		wait()
	end)
	wait()
end

I’ve gone through them, CFrames still confuse me, I can make rotations work by removing the second line within the function that sets the Y value. How could I just set the Y value but not change any other values?

CFrame.new(part.CFrame.X, Y, part.CFrame.Z)

Change y to the new value and set it to what you want to set equal to it, change part to the name of the thing

Post has been edited to be corrected

What do you mean by : CFrame.C do you mean CFrame.X because yes that sets the right height but my issue then is it no longer rotates

You should not use a while loop for this, attachments are pretty good and you can keep the rotation too!
AlvinBlox made an amazing tutorial explaining them.

Edit:

Heartbeat is a loop itself, you don’t need to put it inside a loop.

4 Likes

Using task.wait() instead of wait() is the simplest solution.
You should almost never use wait() because the delay changes with lag.

Tweening is also a good solution:
https://developer.roblox.com/en-us/api-reference/class/TweenService

1 Like