Detecting when the current running script is disabled/destroyed

If a script creates something like a Gui and inserts it elsewhere in the DataModel, how do I clean up those instances when the script/plugin is disabled or destroyed?

The main use cases are for plugins, gear, and free models.
Right now I need a way to properly disconnect my plugins. Here’s the basic idea:

local screenGui = Instance.new("ScreenGui")
screenGui.Parent = game:GetService("CoreGui")
local connection;
connection = script:GetPropertyChangedSignal("Disabled"):Connect(function()
	if script.Disabled then
		connection:Disconnect()
		screenGui:Destroy()
	end
end)

I use what most people call a “Maid” class, and I can disconnect everything in the script on my own, without RBXScriptConnections being disconnected automatically. How expensive is it to keep track of every RBXScriptConnection associated with a script?

Maybe Script should have a method similar to game:BindOnClose() that allows developers to do this.

4 Likes

For the current running script, I don’t think you can do such thing. I may be wrong.

This isn’t that hard to do. You could even make a helper class that you connect to the script so it can watch for it being removed and you can make your connections with it. I’m not aware of a native way to track that.

For plugins I would create a stringvalue on startup and then delete after one second.

That way I can then listen for childremoved and detect wether it has the same name as the stringvalue.

Typed on iPad excuse me for any grammar errors.

I would like to know a method as well.

I’m trying to make a game with a single local-script per client, and loading all the code into module-scripts - so far this is working perfectly. I would assume that one benefit of using this method is that if a hacker were to modify the script, then the whole thing could break, which is something I am ok with, as it could render whatever they were targeting to achieve useless.

However, what I was hoping to do, is detect if a player were to modify the script or destroy it, then proceed to run a function to kick them or teleport them to the start / lobby place of my game (With some kind of ‘error’ message).

CollectionService tags are removed from objects that are destroyed. I use this as a trick as a substitute for a “Destroyed” signal, just use a dummy/complicated CollectionService tag that isn’t used anywhere else.

2 Likes