Do Instance.new() Instances dissappear after script deletion?

I’ve been looking for ways to optimize my game(s) and I’ve ran into a question that I can’t really find an answer to anywhere else on the internet. Now I’m wondering whether Instances that I created with Instance.new() and that have no assigned parent, but are still accesable using variables, get deleted after the script restarts, gets turned off or gets deleted.

Example (because I think how I desribed it is a bit confusing):

local x_velocity = Instance.new("NumberValue") --this is an example of the Instance I tried to describe
local y_velocity = Instance.new("NumberValue")

--and then maybe some functions like
x_velocity.Value = 10
tweenservice:Create(y_velocity, [...] ):Play()

Will the x_velocity and y_velocity instances get deleted alongside the script, or will they remain in-game unused afterwards, which will obviously make the memory of the game unnecesarily higher?
I would appreciate any kind of help.

By the way, the entire reason why I use Instance.new() for some values is because I want to tween those values. I would also appreciate if any of y’all left any better way to do that, that doesn’t involve having to use Instance.New() which would technically also solve my problem.

No the instances themselves won’t get deleted if you delete the script but modifications to the instances within the specific script where it modifies the object via the instance.new function will no longer do anything to the objects BUT whatever changes you already made to the objects in the script will remain

1 Like

Is there a way I could delete them alongside the script? Or is there a better alternative to the instances, which I can also tween? (also I don’t know if this makes a difference, but the parents of the instances are undefined. just making sure that nothing was missed lol)

It depends: If you parent the Instance to another Instance then no, and if you have any strong reference to that Instance it will also not be deleted

An example of a strong reference is a connection that references the Instance inside of it:

local value = Instance.new("IntValue")

game:GetService("RunService").PostSimulation:Connect(function()
	value.Value += 1
end)

If all strong references to the Instance are removed then the garbage collector will remove it on the next cycle

3 Likes

Where are you deleting the script? If the script is deleting itself you can just delete the instances before deleting the script otherwise you could use modules or remotes to keep track of the instances made within another script and delete them then once the script is deleted

1 Like

These strong refererences will also be removed once the script gets deleted aswell, right?

If you destroy the script using either the :Destroy() method or game:GetService("Debris"):AddItem() and the Instance only exists within that script then yes, the strong references should be removed as well

1 Like

For example, when I have a local script within StarterGui, they will get deleted (or atleast reset, I don’t exactly know how roblox handles those) when your character respawns. I’m not sure how I can use modules and remotes to keep track of an Instance whose parent is nil, but I’ll look into it.

Alright, I see. Thanks!
(just now I understood what was meant by strong reference lol)

1 Like

By the way the recent update Roblox made to the dev console should help you keep track of the Instances within your game to make sure they’re being properly cleaned up:

1 Like

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