Is there A way to completely remove a variable

my representation is x=nil does not actually remove the variables

do you literally want to remove the whole var from the script?

I mean, if it’s numbers you can just make a table and remove it from the table but I’m not sure about the ones that actually define paths.

you can still set those to nil normally

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.

yes

then how do i combat memory leaks from them, do i just have to make a temporary scope for the 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.

I would want source but also if that was true, the variable still exists, therefore the variable is still taking the bytes, not its value though

but i really really doubt nil == 0 bytes

A character is 1 byte in lua. I know a variable takes a few bytes too. I don’t think it’s gonna cause a memory leak.

alright i see, but it would still be nice to see if there is any method to completely remove a variable

I don’t think there is. But again, you can remove them by deleting the script if it finishes its task.

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

you really don’t have to worry about removing vars

exactly

Setting to nil is enough,

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.

2 Likes

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

5 Likes

Interesting. I never really knew this, thanks

sometimes i wonder how the lexer works

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