Let $X \subset \mathbb A_{\mathbb Z}^d$ be an affine scheme of finite type over $\mathbb Z$. Let $I$ be the ideal of $\mathbb Z[t_1, ... , t_d]$ corresponding to $X$, generated by $f_1, ... , f_r$. Assume that for every $n \geq 1$, the set of $\mathbb Z/n\mathbb Z$-rational points
$$X(\mathbb Z/n\mathbb Z) = \operatorname{Hom}_{\textrm{sch}}(\operatorname{Spec}\mathbb Z/n\mathbb Z,X) $$ $$= \{ (a_1, ... , a_d) \in (\mathbb Z/n\mathbb Z)^d : f_i(a_1, ... , a_d) \equiv 0 \pmod{n} \textrm{ for } 1 \leq i \leq r\}$$
is nonempty. For $m \mid n$, the ring homomorphism $\mathbb Z/n\mathbb Z \rightarrow \mathbb Z /m\mathbb Z$ induces a map of sets $X(\mathbb Z/n\mathbb Z) \rightarrow X(\mathbb Z/m \mathbb Z)$, so we have an inverse system of sets, induced by the directed system $\mathbb Z_{> 0}$, with $n < m$ if and only if $m$ divides $n$.
The usual construction of inverse limits in the category of sets tells us that $\varprojlim X(\mathbb Z/n\mathbb Z)$ is the usual subset of
$$\prod\limits_n X(\mathbb Z/n\mathbb Z)$$
consisting of compatible sequences. It's easy to see in this case that this set identifies with
$$X(\varprojlim \mathbb Z/n\mathbb Z) = X(\hat{\mathbb Z})$$
What is going on in this process of commuting the inverse limit with the representable functor $\operatorname{Hom}(-,X)$? How general is this identification of inverse limits?