Why do Protectrons sometimes attack me? [duplicate]

This has to do with the mode they're in:

What are the effects of the various Protectron Personality Modes?

If they're in Construction or Law Enforcement mode, you can turn them hostile if you do not meet their requirements.

In Construction mode, you need to be wearing a Hard Hat.

In Law Enforcement mode, you cannot have a weapon drawn.


As a continuation of Nelsons answer, there is also a Subway Attendant mode that you can put the protectron in. Once the protectron is in that mode, you can give him a subway token and he will no longer be hostile to you but will still attack any enemies.