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?
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.
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()
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!
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!