I saw something similar with a script like this. I changed the IF ObjectFlagSet to IF DialogStarted (with the NPC I was talking with and wanted to give the gold to) and it works every time.