Why are you doing your own thing when you were given code that already should do everything right?
You're setting the texture for when you are in a group, but not doing anything to remove it once you're not in a group anymore. So it stays.
Also, I recommend you look up some general guidelines on code quality and readability, and at the least, use [highlight="lua"] tags with proper indentation when adding code snippets to posts.
http://lua-users.org/wiki/LuaStyleGuide