Scripting: Fix a bug where event handlers are not getting cleaned up properly.

This fixes an issue where dynamically removing a script file would cause script
errors if the event handler was not longer defined by any other script file.

It should now be possible to dynamically add and remove script files while
running the game.