Script still exists after being destroyed

Hello everyone!
I have really weird problem with destroying scripts inside gui. So let’s start with how my explorer looks like:

image

Everything begins in Main local script. Here is the code:

local GuiHandler = require(script.GuiHandler)
GuiHandler.run()

script.Destroying:Connect(function()
    print('destroying')
end)

I have a test function in Home module script that prints something when workspace name changes. Look:

workspace:GetPropertyChangedSignal('Name'):Connect(function()
    print('changed')
end)

When tween in CameraHandler module script is completed gui should be destroyed:

function cameraHandler.reset(location, gui)
    local tween = TweenService:Create(camera, SPAWN, {CFrame = location})
    tween:Play()

    tween.Completed:Connect(function()
        camera.CameraType = Enum.CameraType.Custom
        gui:Destroy() --gui references to MainMenuGui
    end)
end

So now the problem:
The gui is being destroyed as it should be however connections inside scripts are not being disconnected and script.Destroying is not being triggered. Here is the video:

As you can see the connections still trigger print and it says that the source is “Studio”. Note that I’m not using any task functions and coroutine s. I appreciate any help!

Couldn’t you use script.Enabled = false or script.Disabled = true before destroying it?

First of all this solution is sketchy as hell because scripts will still exist in memory. Secondly it doesn’t help either.

I tried destroying local script instead of gui but module script were still running.

1 Like

maybe you can try using debris ?

Also I did some research and I think if you don’t return the module then the function inside will run forever or soemthing idk

1 Like

Does not work either.

charlimit

Yeah that solves my problem. I created new gui with just local script and module script and problem occurred again.

So I guess I have now 3 solutions for that problem:

  • place modules inside ReplicatedStorage
  • hide gui instead of destroying it
  • manually disconnect events (this one won’t work 100% properly because old modules will still exist they will be just “deactivated” if you disconnect everything)

Thanks for the help!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.