You can write your topic however you want, but you need to answer these questions:
I want this NPC to face the target before it attacks, the problem is that when I have the function to make the NPC face the target, before it attacks the NPC looks like he teleports towards the target. This bug can only be seen by the players though, watching the NPC chase from the server’s view is how I want it to look.
I have already tried making the function run after the :movetofinished.Wait() but if the NPC is next to the target and isn’t moving, the NPC wont face the target.
Down below is the function to make the NPC face the target before attacking, and the code that makes the NPC move towards the target is just a simple Humanoid:MoveTo() command
task.spawn(function()
local direction = Vector3.new(target.Position.X, hrp.Position.Y, target.Position.Z)
for i = 0, 0.9, 0.05 do
hrp.CFrame = hrp.CFrame:Lerp(CFrame.new(hrp.Position, direction), i)
task.wait()
end
end)
This code is just to demonstrate how it’s done, you will need to incorporate this with your pathfinding system and make the NPC look at the closest target.
Just added it and looks like it works, but I still have the same problem where the NPC looks like he teleports to the target through the client, but watching it from the server looks fine
Apparently if you rotate the NPC while it’s moving, it will teleporting through the client view, I just had it so that if the NPC isn’t moving it will rotate as so:
local hrp = NPC:WaitForChild("HumanoidRootPart")
if math.floor(hrp.Velocity.Magnitude) == 0 then
hrp.CFrame = CFrame.lookAt(hrp.Position, target.Position * Vector3.new(1, 0, 1) + hrp.Position * Vector3.new(0, 1, 0))
end
Also the NPC will automatically face the target when using the MoveTo so there is no need to make him face the target before moving.