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
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...
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
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.
I'm gone at the moment, but I'll be sure to try your method and update you if it works; thanks for helping!