Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Do You Properly Destroy a Script?

So I recently found out that scripts containing loops or connected functions will still be active despite being deleted. I've searched through numerous forums and conversations about this and have found that it's best to disconnect() or break any code that's no longer in use. I've also found that when destroy() is used on a script's parent, whatever connections are present in the script code that's being deleted will be disconnected (I know I said "connected functions will still be active despite being deleted" but that's what some people have said.) Anyway, I'm somewhat confused as to how to properly destroy a script and its code when no longer in use. And I want to know whether or not connected functions should be disconnected when no longer in use or is it best just to leave it as is when the script is deleted; moreover, I want to know if my efforts at breaking a while loop and a connected function are effective or poor. Not sure if many people will understand what I'm trying to accomplish or understand but if someone could help, then I would greatly appreciate it!

Here's my attempt at breaking a loop when its parent is deleted:

	local Rotate = coroutine.wrap(function()
			while wait() do
				if script.Parent ~= nil then --checks if the parent is not nil
					Particle.Dirt1.Rotation = Particle.Dirt1.Rotation + 0.1 ---all this is just for some particle custom particle system
					Position_Value.Value = Position_Value.Value + Vector3.new(0,40,0)
				else
					print("broke worked 2") --wanted to see if it'll actually printed something and it did not
					break --if nill then loop is no longer used
				end
			end
		end)
		Rotate()

And here's my attempt at disconnecting a heartbeat function when its parent is deleted:

local GoDownDo = nil
GoDownDo = game:GetService("RunService").Heartbeat:Connect(function()
	if script.Parent ~= nil then -checks if parent is not nill
		for i, part in pairs(script.Parent:GetChildren()) do
			if part:IsA("Part") and part.Name == "Heavy_RAINL" then 
				if part.Position.Y <= -5311.475 then
					part:Destroy()
				else
					local Position_Val = part:FindFirstChild("PositionVal")
					Position_Val.Value = Position_Val.Value - Vector3.new(0,20,0)
					part.CFrame = CFrame.new(Vector3.new(Position_Val.Value.X,Position_Val.Value.Y,Position_Val.Value.Z))
				end
			end
		end
	else
		GoDownDo:Disconnect() --disconnects the function is the parent is nil
	end
end)

4 comments

0

Possibly a dumb question, but are you changing the script's disabled property before deletion? I.E. script.Disabled = true [THEN] script:Destroy() Of course, this would be inconvenient if, as you said, you would want to destroy a model or folder containing several scripts, so you'd have to loop this for every script...

0

Not sure if you're going to see this but no I'm not changing neither both of the script's property above, both scripts above have a line that deletes its parent (hence why I added the if statement for the parent being nil or not) but I didn't include that part when showing my attempts

0

I forgot to specify, that what I mentioned could resolve this problem, as the Disabled property (AS FAR AS I KNOW) should completely yield the script. Let me know if doing so works for you.

0

I'm gone at the moment, but I'll be sure to try your method and update you if it works; thanks for helping!

1 answer

Funnily enough, I've actually just reached a scenario where this mechanic is favorable. The Humanoid.Died event is notoriously unreliable, so I've discovered that by putting in a character script that detects if it's parent becomes nil, I can then fire my own death event. However, since this also means the script is running, I do script.Disabled = true to stop it. probably you could also do script:Destroy() as well, but only after that. Here is the code for my dumb little death detector ;>

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DeathEvent = ReplicatedStorage.CharacterDied

script.Parent.Humanoid.Died:Connect(function()
	DeathEvent:Fire()
end)

script.Parent.Changed:Connect(function()
	if script.Parent == nil then
		DeathEvent:Fire()
		script.Disabled = true
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.