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
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)
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
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
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.
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: