NPC followers turn to the side after I stop walking?

Nope, just a non anchored rig with rig joints.

Yea, atleast its not too hard of a fix unless there is no way for a primary part.

Yes, If this was not the problem before then I would have just done.
CFrame(npc.pos, player.pos)
Unfotunatly CFrames gets pretty complicated. My method is trying to change the y rotation while keeping the rest the same. I assume your work around had something similar?

Same here! I changed lookAt to this: NPC.HumanoidRootPart.CFrame = CFrame.lookAt(NPC.HumanoidRootPart.Position, (NPCPos * Vector3.new(1, 0, 1)) + Vector3.new(0, NPC.HumanoidRootPart.Position.Y, 0))

But I have absolutely no idea why it didn’t work. Probably earlier parts of the code, not using CharacterAdded is breaking this.

1 Like

Maby, I wonder what is says in the output.


this is what it says

Sorry for the big delay, I’m finally on my computer and I’ve made a solution.
Depending on how you plan to use your NPC, if your game is multiplayer or you want a more readable script then you may want to use this script I’ve created.
This script will follow the closest player and do everything you asked.

local NPC = script.Parent

while wait() do
	local Target = nil
	local TargetDistance = math.huge
	for i = 1, #game.Players:GetChildren() do
		local Player = game.Players:GetChildren()[i]
		local Character = Player.Character
		if Character then
			local Distance = (Character.PrimaryPart.Position - NPC.PrimaryPart.Position).Magnitude
			if Distance < TargetDistance then
				Target = Character
				TargetDistance = Distance
			end
		end
	end
	if Target ~= nil then
		if TargetDistance > 6 then
			NPC.Humanoid:MoveTo(Target.PrimaryPart.Position)
		else
			local function PointTowards2D(CF1 : CFrame, CF2 : CFrame) : CFrame
				local V1 = CF1.Position
				local V2 = CF2.Position + Vector3.new(0, -CF2.Position.Y + CF1.Position.Y, 0)
				return CFrame.lookAt(V1, V2)
			end
			NPC:PivotTo(PointTowards2D(NPC.PrimaryPart.CFrame, Target.PrimaryPart.CFrame))
		end
	end
end

Otherwise you can still use your script and just replace the part I was doing with:

local function PointTowards2D(CF1 : CFrame, CF2 : CFrame) : CFrame
	local V1 = CF1.Position
	local V2 = CF2.Position + Vector3.new(0, -CF2.Position.Y + CF1.Position.Y, 0)
	return CFrame.lookAt(V1, V2)
end
NPC:PivotTo(PointTowards2D(NPC.PrimaryPart.CFrame, otherNPC.PrimaryPart.CFrame))

The PointTowards2D makes a CFrame point from one vector to the other only on the Y axis.
It works by setting the Y position on the second vector to be the same as the one in the first.

I recommend not using CFrame.new(Vector3, Vector3). It has been deprecated and you should now use CFrame.lookAt(Origin, LookAt). I recommend editing your post.

1 Like

Ill make the change, I didn’t know it was deprecated thanks!
I’ve also done some research on :SetPrimaryPartCFrame() vs :PivotTo() And it sounds like :PivotTo() has better performance

And you mentioned earlier how you need a primary part for :SetPrimaryPartCFrame()

You dont need one for :PivotTo()

Hi, sorry for not answering quickly. I don’t know why but the DevForum just didn’t give me the notification for your reply, I just found it randomly by checking here again. I’ll try what you mentioned, thanks in advance!

I tested the first script out, this happened:

The NPCs follow the player correctly and the direction isn’t messed up or anything, but sometimes it’s a bit choppy and when the player stops, they continue to get close to the player and then teleport backwards

I would send a video, but, boo-hoo, DevForum is being trashy as always!
image

What script are you running it in, a server script or a local script?
If its a local script, is the model also local?

server script, and it’s inside the npc

Are you able to create a base plate with just the npc and the script, send it to me and I can take a closer look at it?