How to make AI notice player when in range and attack

Alright, i need some scripting help on how to make a AI notice a player when he gets within a certain range, and either shoot at him, or run at him. This would be extremely useful for me.

Also i would prefer if it was in JavaScript, as that is the language i understand the most, and will be the easiest for me to understand what you did, so i don’t have to keep asking stupid questions.

Thanks to everyone who can help.

The robot guard script has the basic elements you need…