Instead of using that while statement, you may loop using for through all players then check the range with entity_range instead of getting each entity classname.
What kind of error are you talking about? Infinite loop? Then try using for statement.
Segmentation fault? Make sure you are using remove_entity properly.