Help with understanding Destroy() function

I have a question about the destroy function. So I’m just referencing a part in workspace called Origin and I then destroy the variable. When I print the variable, it actually prints “Origin” instead of nil which is making me confused since I thought Destroy is supposed to make it nil. Can someone please explain what I’m missing? thx

local variable = workspace.Origin
variable:Destroy()
print(variable)

:arrow_up: prints “Origin”

4 Likes

Have you checked on the dev api on this.

There was an explanation for it there.

1 Like

The variable caches all the part data, although it seems you can be grabbing things from the part, it’s actually the variable. Roblox should actually fix this super easily, but for now if the object is a variable, just put it to nil. I don’t think asking if the part’s parent works all the time, since I got errors sometimes since it was ACTUALLY nil. So what you can do is detect if it’s there then detect if it has a parent.

Yea I read up on it but I was still slightly confused

I’m certain that is not how it works. I believe that Destroy sets the parent to nil and disconnects all signals. It then waits for the garbage collector to pick it up once all tables indexing the instance remove it.

EDIT: The destroy page covers this (p.s. i was pretty much correct) Instance:Destroy

2 Likes

The variable is a reference to a Luau userdata which is used to interace with the Roblox instance that exists on the C++ side. When Destroy is called on a Luau userdata which represents a Roblox instance the object’s ‘Parent’ property is locked (set to read-only) and set to ‘nil’ and any ‘RBXScriptConnection’ objects (connections) made via its ‘RBXScriptSignal’ objects (events/signals) are disconnected. The Luau userdata continues to exist allowing for the Roblox instance to still be interfaced with.

local Part = Instance.new("Part")
Part:Destroy()
print(Part.Position) --0, 0, 0
print(Part.Size) --4, 1.2, 2
1 Like

It just delete the part or anything e.g. workspace.TestPart:Destroy()

OK so basically calling destroy on a variable still allows for the backend data to exist? And the best way to deal with this is just setting the variable to nil instead? Destroy just removes objects from the physical space however it doesn’t actually clear that data, is this accurate

The documentation page has everything you need.

Calling :Destroy() on an Instance will not clear the variable, but data related to the Instance itself, to prevent your script from accesing data from that Instance, simply set it to nil.

local part = Instance.new("Part")
part.Name = "Hello, world"
part:Destroy()
-- Don't do this:
print(part.Name) --> "Hello, world"
-- Do this to prevent the above line from working:
part = nil

(Taken from Engine API docs.)
This example explains exactly your inquiry in detail.

I suggest you look further and do research before posting a forum topic.

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