NPC Moving to last humanoidRootPart Positon

I am trying to make a NPC to follow the player. It is following the player, but slightly. Because I am trying to fix this problem, where the NPC would move to the last playerRootPart position instead of the current one. So basiclly if the player moves, the npc would move where the last position was, it isn’t following along with the player.

What I have tried:
I tried creating a recompute function, where if the player position is greater than its current position then create a new path. But it isn’t doing that.

Here is the script:

local PathfindingService = game:GetService("PathfindingService")

local AttackNPC = script.Parent
local zombie = AttackNPC:FindFirstChild("Humanoid")
local NPCRootPart = AttackNPC:FindFirstChild("HumanoidRootPart")
local isNPCBlocking = AttackNPC:FindFirstChild("isNPCBlocking")

local punch1 = AttackNPC:FindFirstChild("Combo1")
local punch2 = AttackNPC:FindFirstChild("Combo2")
local gettingHit = AttackNPC:FindFirstChild("GettingHit")

local attackCooldown = 0.7
local damage = 2.5
local hitSpeed = 2

local RecomputePathFrequency = 1

LastRecomputePath = 0
local Recomputing = false

charactersFound = false 
nearestCharacterFound = false
target = nil
local canAttack = true

whoHitNPC = {}

local function checkDistance(part1,part2)
	magnitude = (part1.Position - part2.Position).Magnitude
	return magnitude
end

function scanningForTarget()
	for i,v in pairs(game.Workspace:GetChildren("Model")) do
		if v:IsA("Model") then
			if v ~= AttackNPC then
				if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") then
					local playerCharacter = v
					characterRootPart = v:FindFirstChild("HumanoidRootPart")
					charactersFound = true
					nearestCharacterFound = false
					moveToTarget()
				end
			end
		end
	end
end

function moveToTarget()
	if checkDistance(NPCRootPart,characterRootPart) < 100 then
		target = characterRootPart.Position
		nearestCharacterFound = true
		path = PathfindingService:CreatePath()
		path:ComputeAsync(NPCRootPart.Position,characterRootPart.Position)
		local waypoints = path:GetWaypoints()
		for _,waypoint in pairs(waypoints) do
			RecomputePath()
			zombie:MoveTo(target)
			zombie.MoveToFinished:Wait()
			if characterRootPart.Parent.Humanoid.Health <= 0 then
				target = nil
				scanningForTarget()
			end
		end
	end
end

function RecomputePath()
	wait(RecomputePathFrequency)
	if target >= target and Recomputing == false then
		Recomputing = true
		path:ComputeAsync(NPCRootPart.Position,characterRootPart.Position)
		wait(RecomputePathFrequency)
		Recomputing = false
	end
end



function Attack()
	local currentWalkSpeed = target.Parent.Humanoid.WalkSpeed
	if checkDistance(NPCRootPart,characterRootPart) < 3.5 then
		if target ~= nil and zombie.Health > 0 and canAttack == true then
			local combo1Track = zombie:LoadAnimation(punch1)
			combo1Track:Play()
			target.Parent.Humanoid:TakeDamage(damage)
			hitTrack = target.Parent.Humanoid:LoadAnimation(gettingHit)
			hitTrack:Play()
			target.Parent.Humanoid.WalkSpeed = hitSpeed
			hitTrack:Stop()
			wait(1)
			target.Parent.Humanoid.WalkSpeed = currentWalkSpeed
			wait(attackCooldown)
			if checkDistance(NPCRootPart,characterRootPart) < 3.5 and canAttack == true then
			local combo2Track = zombie:LoadAnimation(punch2)
			combo2Track:Play()
				target.Parent.Humanoid:TakeDamage(damage)
				hitTrack:Play()
				target.Parent.Humanoid.WalkSpeed = hitSpeed
				hitTrack:Stop()
				wait(1)
				target.Parent.Humanoid.WalkSpeed = currentWalkSpeed
			end
		end
	end
end


function main()
	if target ~= nil and nearestCharacterFound == true and target.Parent.Humanoid.Health > 0 then
		moveToTarget()
		Attack()
		return true
	elseif target == nil then
		scanningForTarget()
	end
end


while wait() do
	main()
end

Alright I just fixed the problem after changing some things. So basiclly what I did was I removed the recompute path function. I also changed what target is supposed to be. I changed things with the waypoints and stuff. If you wanna see what I changed here is the script:

local PathfindingService = game:GetService("PathfindingService")

local AttackNPC = script.Parent
local zombie = AttackNPC:FindFirstChild("Humanoid")
local NPCRootPart = AttackNPC:FindFirstChild("HumanoidRootPart")
local isNPCBlocking = AttackNPC:FindFirstChild("isNPCBlocking")

local punch1 = AttackNPC:FindFirstChild("Combo1")
local punch2 = AttackNPC:FindFirstChild("Combo2")
local gettingHit = AttackNPC:FindFirstChild("GettingHit")

local attackCooldown = 0.7
local damage = 2.5
local hitSpeed = 2

charactersFound = false 
nearestCharacterFound = false
target = nil
local canAttack = true

whoHitNPC = {}

local function checkDistance(part1,part2)
	magnitude = (part1.Position - part2.Position).Magnitude
	return magnitude
end

function scanningForTarget()
	for i,v in pairs(game.Workspace:GetChildren("Model")) do
		if v:IsA("Model") then
			if v ~= AttackNPC then
				if v:FindFirstChild("Humanoid") and v:FindFirstChild("HumanoidRootPart") then
					local playerCharacter = v
					characterRootPart = v:FindFirstChild("HumanoidRootPart")
					charactersFound = true
					nearestCharacterFound = false
					if characterRootPart then
						moveToTarget()
					end
				else
					print("Can't find target")
					scanningForTarget()
				end
			end
		end
	end
end

function moveToTarget()
		target = characterRootPart
		nearestCharacterFound = true
		path = PathfindingService:CreatePath()
	path:ComputeAsync(NPCRootPart.Position,characterRootPart.Position)
	if checkDistance(NPCRootPart,characterRootPart) < 15 then
		if path.Status == Enum.PathStatus.Success then
			print("Found path!")
		local waypoints = path:GetWaypoints()
			for _,waypoint in pairs(waypoints) do
				waypoint = target.Position
			zombie:MoveTo(waypoint)
			zombie.MoveToFinished:Wait()
		if characterRootPart.Parent.Humanoid.Health == 0 then
			print("Target dead")
				target = nil
				scanningForTarget()
				end
			end
		end
	end
	if path.Status == Enum.PathStatus.NoPath then
		print("No path, looking for a new one")
		scanningForTarget()
	end
end


function Attack()
	local currentWalkSpeed = target.Parent.Humanoid.WalkSpeed
	if checkDistance(NPCRootPart,characterRootPart) < 3.5 then
		if target ~= nil and zombie.Health > 0 and canAttack == true then
			local combo1Track = zombie:LoadAnimation(punch1)
			combo1Track:Play()
			target.Parent.Humanoid:TakeDamage(damage)
			hitTrack = target.Parent.Humanoid:LoadAnimation(gettingHit)
			hitTrack:Play()
			target.Parent.Humanoid.WalkSpeed = hitSpeed
			hitTrack:Stop()
			wait(1)
			target.Parent.Humanoid.WalkSpeed = currentWalkSpeed
			wait(attackCooldown)
			if checkDistance(NPCRootPart,characterRootPart) < 3.5 and canAttack == true then
			local combo2Track = zombie:LoadAnimation(punch2)
			combo2Track:Play()
				target.Parent.Humanoid:TakeDamage(damage)
				hitTrack:Play()
				target.Parent.Humanoid.WalkSpeed = hitSpeed
				hitTrack:Stop()
				wait(1)
				target.Parent.Humanoid.WalkSpeed = currentWalkSpeed
			end
		end
	end
end


function main()
	if target ~= nil and nearestCharacterFound == true and target.Parent.Humanoid.Health > 0 then
		moveToTarget()
		Attack()
		return true
	elseif target == nil then
		scanningForTarget()
	end
end


while wait() do
	main()
end