NPCs with "passing" Dialogue

Basically, I want to have it so that when the player character moves near/past an NPC they have a floating box above their head showing lines of dialogue that are different each time the player passes. What sorts of systems need to play together to get something like this working? I need a starting point as I don’t even know what to Google for something like this.