How to have a part follow a character

alright so i have a character that moves along a path using Humanoid:MoveTo and this path has turns

so what i want is to have a part that is constantly like 5 studs in front of that character

now ive tried something like this:

	while true do
		task.wait()
		part.CFrame = char.HumanoidRootPart.CFrame + Vector3.new(0,0,5)
	end

so this works, but there are 90 degree turns on this path and when he turns, the part is no longer in front of him, this is because it would now need to be 5 on the x axis and not the z axis, because of the turn

1 Like

Do it on RenderStep event, without the while loop and from humanoidRootPart.CFrame
Or weld the part to the humanoidRootPart with the Vector3.new(0,0,5) offset

1 Like

alright i put it in a renderstepped event and also i am using the humanoidrootpart i just forgot to type that sorry, but this still doesnt fix the issue that its on the wrong axis when turning

try this:

while true do
	task.wait()
    local position = (part.CFrame * CFrame.new(0, 0, -5).p)
    local newCframe = CFrame.new(position, part.Position)

    char:WaitForChild("HumanoidRootPart").CFrame = newCframe
end

this uses CFrame instead of just changing the position, so the axis should change too

1 Like

tried it and it pushes the character back and makes it get stuck in place

also studio doesnt seem to like .p

This should work though, you should consider using a weld constraint instead of using a loop, and that script would work but just change “+” to “*” and change “Vector3” to “CFrame”

how does it push it back? maybe turn off cancollide on the part or something

I’d recommend lerping its CFrame to keep things looking smooth:

local character = -- character to follow
local part == -- part
local offset = CFrame.new(0, -2, -3)
local speed = 0.15

local root = character.HumanoidRootPart

root:GetPropertyChangedSignal("Position"):Connect(function()
    part.CFrame = part.CFrame:Lerp(root.CFrame * offset, speed)
end)

Parts of this are borrowed from this post, and the code above is untested. It should, though, give a solid idea of what you’re doing.

2 Likes
game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(chr)
        local hrp = chr:WaitForChild('HumanoidRootPart')
        local Part = Instance.new('Part', hrp)
        Part.CFrame = hrp.CFrame * CFrame.new(0,5,0)

        local Weld = Instance.new('WeldConstraint', Part)
        Weld.Part0 = Part
        Weld.Part1 = hrp
    end)
end)

This should work.

1 Like

Here is a better example of how you can make the part follow your character, don’t forget to put the template of your part

local partOffset = Vector3.new(0, 0, 5)  -- Offset from the character's position
local partTemplate = -- Your part template here
local humanoidRootPart = char.HumanoidRootPart

-- Create the part
local part = partTemplate:Clone()
part.Parent = workspace -- Put it in the right place

while true do
    task.wait()
    
    -- Calculate where the part should be
    local targetPosition = humanoidRootPart.CFrame.Position + humanoidRootPart.CFrame.LookVector * partOffset.Z
    
    -- Adjust the position based on the character's rotation
    local characterRotationY = humanoidRootPart.CFrame.YRotation
    local rotationOffset = Vector3.new(math.sin(characterRotationY), 0, math.cos(characterRotationY)) * partOffset.X
    targetPosition += rotationOffset
    
    -- Move the part smoothly towards the target position
    part.Position = Vector3.Lerp(part.Position, targetPosition, 0.2) -- Adjust the speed as needed
end

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