I put the variable defining script.parent in a table and tried to remove it which worked. Iām not sure if using it to define things will work.
Edit: You cant destory local variables.
A variable is just a few bytes. It wonāt affect your game unless you spam long variables. You can just delete useless scripts. Plus, setting it to nil will make it 0 bytes.
But this is not so ideal, sure it wipes all variables, but with that method you cant wipe a specific variable, nothing like that, i think you just do temporary scopes or nothing at all if you want specific variables wiped
If you are worried and want to know 100% sure for any type of script use debug.setmemorycategory(āSome Nameā) to monitor the script memory usage in /console and see if the memory drops after some time.
It seems like even if you are creating instances and not destroying them they get destroyed because they become out of scope and not used anymore which Roblox identifies, you should verify twice but I believe itās true.
Anyways you should just make sure to :Destroy them or set the key = value to nil just in case.
It may look like the variable is still there, but it is not. If a value is nil, it is nothing.
They are treated the same as variable that never existed in the first place.
local x = {}
print(x) -- {}
x = nil
print(x) --> nil
print(nonexistantvariable) --> nil
print(something) --> nil
something = 1
print(something) --> 1
Good observation! Even though it doesnāt have a value, the Lua bytecode still contains a definition for āX is a local variable in this scopeā. Thatās the compiled script. Thereās nothing you can do about it, but it wonāt cause memory leaks. Memory leaks are when memory piles up endlessly without getting removed, but it only says āX is a local variable in this scopeā exactly once in the bytecode. Even if you have a while loop or call a function with a local variable inside, it wonāt create a longer compiled bytecode script. Once the variable is set to nil or the variable goes out of scope, any memory it does use (outside of the bytecode) will be cleared. No memory leaks!
Edit to add some clarification. The compiled code doesnāt check if itās a local variable before making it local/global. The compiler says āHey, weāve already got this declared as local, so the compiled code will always act as if X is local in this scope.ā Thatās why you canāt just delete a local variable. The concept of local/not local doesnāt exist in compiled code. This is what the compiled code looks like with the script you provided:
Bytecode in English
load 5 into register
load nil into register
get global print
move register value to pass it to print
call print
load 3 into register
get global print
move register value to pass it to print
call print registers go out of scope and become nil
get global print
get global x (nil because we never assigned a global x)
call print with this value
The compiler here clearly treats our global x differently from our local x. The bytecode has been compiled with the knowledge that local x is or isnāt valid in a certain scope.
Hereās the code if you remove the local and make x global.
Bytecode in English
load 5 into register
set x to that value
load nil into register
set x to that value
load global print
load global x
call print with x
load 3 into register
set x to that value
get global print
get global x
call print with x we are now out of scope
get global print
get global x
call print with x