Why does the NPC work so strangely?

To force the NPC to teleport to the player in front of his face, and then approach using Humanoid:moveTo().

NPCs sometimes teleport to a location to the left or right of the player instead of appearing directly in front of him and approaching him. Such inconsistent behavior violates the intended behavior of the NPC.

    if target then
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {target}
		local targetPosition = target.HumanoidRootPart.Position + target.HumanoidRootPart.CFrame.LookVector * 13
		local raycast = workspace:Raycast(targetPosition, Vector3.new(0, -1000, 0), raycastParams)
		if raycast then
			script.Parent.HumanoidRootPart.Position = targetPosition
		end
		script.Parent.Humanoid:MoveTo(target.HumanoidRootPart.Position)
	end
	wait(1)
	if target then
		local nearestPoint = FindClosestPoint(target)
		if nearestPoint then
			script.Parent.HumanoidRootPart.CFrame = nearestPoint.CFrame
		end
	end
	wait(1)
1 Like