Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Accessing an object that is parented to nil?

Asked by 8 years ago

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

2 answers

Log in to vote
0
Answered by 8 years ago
Edited 8 years ago

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.

Ad
Log in to vote
0
Answered by
Sublimus 992 Moderation Voter
8 years ago
Edited 8 years ago

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

Answer this question