The book didn't cover a lot about ni but I know that it is used to tell what time it is or where you are going. but nothing like this. the best explanation I found was that it could also be used as an indirect object marker. but how and why that works I don't understand. As said before, we didn't cover a lot about ni. Thank you in advance
Too long for a comment so I write it as an "answer".
As you said, に can be used as an indirect object marker. For example: I write an email to my colleague.
- email is the direct object
- colleague is the indirect object
As far as I know there are some "special words", which call for に as a particle. ...聞く (to ask) or ...会う (to meet) [Perhaps someone else can extend this list] So in your case, we have "ask the teacher". 先生に聞きました.
Note that this does not mean, that you can always use に with this "special words" as a particle, if other rules apply. (In my opinion these cases are very rare^^)
You're right that this particular に (ni) is the indirect object marker. 聞く (kiku) is a transitive verb which means "to ask" in this context. The object it takes is the thing that is asked rather than the person being asked, so it can take both に (ni) and を (wo) at the same time e.g.
sensei ni shitsumon wo kikimashita
I asked teacher a question.
As for why the person your asking requires に, well that's just a quirk of this verb (kiku) that you need to learn.