Is there a function for writing a script when a window has been closed eg.(Window.onClose())
Yes, every window has event scripts, documented here.