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

Calling :Destroy() on a variable that references a object what happens?

Asked by 5 years ago

So I've been thinking, does calling :Destroy() on a variable with a object reference make it turn nil? or is it gone forever.

For example:

local part = game.Workspace.Part

part:Destroy()

print(part.Name)

--Does it result in nil or does it make the variable "part" non existent? (garbage colletion)

Thanks for viewing this!

2 answers

Log in to vote
2
Answered by 5 years ago

While there is still a reference to the part, it will not be deleted by the garbage collector. All the Destroy() function does is it set's the instance's parent to nil and locks it to nil so it cannot be reassigned to another parent.

If you want it to be collected by the garbage collector, set the variable to "nil" and remove any other references to that part and the garbage collector will get to it.

local part = game.Workspace.Part

part:Destroy() --//Locks parent to nil

print(part.Name) --//You can still print the name because the object still exists, but it's parent is locked to nil

part = nil --//Now that there is no reference to the part, the garbage collector will get it because there is no way to get the part ever again.
0
Good answer, why are they underrated. You deserve a lot programmerHere 371 — 5y
Ad
Log in to vote
0
Answered by
0_2k 496 Moderation Voter
5 years ago

It results in it being non existent

https://developer.roblox.com/en-us/api-reference/function/Instance/Destroy

Answer this question