How to run function when studio closes?

Hello guys, I’m making plugin. When you use it, it creates folder in workspace with brush, plane and plane orientation marker. I want to delete them, when studio closes.

How I can detect when user closes his studio?

I can tell you that it is difficult to detect when a user is closing Roblox Studio as there is no built-in feature or method for it. Additionally, the user may close the application abruptly without giving any indication, making it even harder to detect.

However, certain clues can indicate that the user is no longer using the Studio, such as a sudden drop in activity or network traffic - I’m unsure how you’d detect these. Nevertheless, detecting when a user has closed Roblox Studio is not a foolproof process, and it is always better to communicate with the user directly to ensure that the session has ended properly; you could have a box that they can check to save their work before they end their session.

I think ppl just leave the folder, but they create it in server storage. Like all the big plugins: toolbrush, archimedes, moon2

There is a plugin event for this, it doesnt only fire for leving studio though so if thats an issue in your code watch out for that. I was just looking for something like this too and found this, in my case its perfect: docs