Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Accessing an object that is parented to nil?

How will I do this? Probably going to use for security reasons

2 answers

You can only access an object whose parent is nil if you have a reference to it; i.e. you have a variable containing that object, meaning you must create a variable containing the object before setting its parent to nil.

If an object's parent is nil and there are no references to it (either variables in scripts or properties of other instances, such as an ObjectValue), it will be garbage collected, meaning it will no longer exist at all.

To expand on IDidMakeThat's answer (which is entirely correct), if you set the Parent of an instance to nil, you must maintain a pointer to that instance in your code.

The reason for this is, when you create an instance in your code, it get's a specific address in memory, i.e. 0x02150612. When you create a pointer to this instance (a variable in Lua), the garbage collector (see linked page) will not remove the instance from memory because it understands that you are still using it. However, if you do not have a pointer to the object, it will get deleted from memory by the garbage collector to free up space in memory.

And in code:

Instance.new("Part", nil) -- Create the part, and set parent to nil.
-- Where do we find the part? We did not create a pointer, nor does it have a parent?
-- We can't find it, because it has been garbage collected!

What would work:

lol = Instance.new("Part", nil) -- Creates the part
print(lol) -- This will print the memory address
-- Lots of random code, but never setting lol equal to anything else
print(lol) -- This will still print the memory address

Basic Memory Management on Roblox

This discussion is preserved as it was. New answers, comments, and votes are closed.