How to make part follow the player (Like a pet)

Hey! Recently I’ve been working on a co-op game where one player is small and the other is regular size. I want to have a system where the regular sized character has a seat that follows them so the baby can hop on and be taken where the regular sized person goes. I have most of the script done, I just need help optimising it and making it always be 2 studs behind the player’s hrp. Any help on this is greatly appreciated!

Current script:

while true do
	local h = script.Parent.Parent:FindFirstChild("Humanoid")
	if h then
		local seat = script.Parent
		local char = seat.Parent

		local BG = Instance.new("BodyGyro", seat)
		local BP = Instance.new("BodyPosition", seat)

		BG.MaxTorque = Vector3.new(math.huge,math.huge,math.huge)
		BP.MaxForce = Vector3.new(math.huge,math.huge,math.huge)

		while task.wait() do
			BP.Position = char.HumanoidRootPart.Position + Vector3.new(0,0,-2)
			BG.CFrame = char.HumanoidRootPart.CFrame
		end
	end
	task.wait()
end
3 Likes
local RunService = game:GetService("RunService")

local seat = script.Parent
local char = seat.Parent
local h = char:FindFirstChild("Humanoid")

if h then
    local BG = Instance.new("BodyGyro", seat)
    local BP = Instance.new("BodyPosition", seat)

    BG.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
    BP.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
    BP.D = 1000
    BP.P = 10000
    RunService.Heartbeat:Connect(function()
        if char and char:FindFirstChild("HumanoidRootPart") then
            local hrp = char.HumanoidRootPart
            BP.Position = hrp.Position - hrp.CFrame.LookVector * 2
            BG.CFrame = hrp.CFrame
        end
    end)
end
2 Likes

Don’t do that, there’s a thing called align position for doing exactly what you want to achieve without needing to write any sort of code

4 Likes

you can try something like this:

local Character = script.Parent
local Root = Character.HumanoidRootPart

local Offset = Vector3.new(0, 0, 2)

local FollowAttachment = Instance.new("Attachment")
FollowAttachment.Parent = Root
FollowAttachment.CFrame = Offset

local Seat = game.ReplicatedStorage.Seat:Clone()
Seat.Parent = Character
Seat.AlignPosition.Attachment1 = FollowAttachment
Seat.AlignOrientation.Attachment1 = FollowAttachment

and if you already have the seat in the character model you can do this

local Character = script.Parent
local Root = Character.HumanoidRootPart

local Offset = Vector3.new(0, 0, 2)

local FollowAttachment = Instance.new("Attachment")
FollowAttachment.Parent = Root
FollowAttachment.CFrame = Offset

local Seat = Character.Seat
Seat.AlignPosition.Attachment1 = FollowAttachment
Seat.AlignOrientation.Attachment1 = FollowAttachment

Youll just need to put an AlignPosition and AlignOrientation constraint in your seat model, and set both of their Attachment0’s to an attachment in the Main part of the seat you want to move.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.