I’m trying to make the NPC Follow the player, but when the distance between the NPC and the player becomes short the NPC starts shuddering.
here is the code
local npc = script.Parent
local humanoid = npc.Humanoid
local HRP = npc.HumanoidRootPart
local runSpeed = 22
local followDistance = 60
local attackDistance = 4
function Follow(hrp)
humanoid.WalkSpeed = runSpeed --make npc run
humanoid.WalkToPoint= hrp.Position
end
function Repeat2(npc, crrPlr)
local currentPlayer = crrPlr -- the player character that the NPC should follow
repeat
local dis = (currentPlayer.HumanoidRootPart.Position - HRP.Position).magnitude -- calculate distance bteween the npc and the spoted player
if dis < followDistance then
--if the target is not in the attack dis then follow him
spawn(function()
if dis >= attackDistance then
Follow(currentPlayer.HumanoidRootPart)
else
npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position) --stop the npc from moving
end
end)
end
wait()
until currentPlayer.Humanoid.Health <= 0 or dis > followDistance or game.Players:FindFirstChild(currentPlayer.Name) == nil
end