Notation(from Kunnen): $M$ is countable transitive model of $ZFC$, $P$ is a partial order that belongs to $M$, $G$ is a $P$-generic filter over $M$, $\hat{b}$ will be a $P$-name for $b\in M$, and $M[G]$ is the smallest model of $ZFC$ containing $G$ and $M$.
The exercise i'm trying to do is the following: If $f$ is a function with range contained in $M$ and $f$ belongs to $M[G]$ then there is a set $B$ that belongs to $M$ containing the range of $f$. The hint given is to consider
$B=\left \{ b:\exists p \in P(p\Vdash \hat{b} \in \operatorname{Rng}(\tau ) ) \right \} $ where $\operatorname{val} (\tau,G) = f$
I understand that this describes the desired $B$ as a class in $M$ but i can't find a way to show that this is actually a set in $M$.
Thanks for the help and sorry for the trivial question.