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

Why doesn't the 'Destroy' method truly Destroy an Instance?

Asked by 9 years ago

I was thinking, Does the 'Destroy' method truly destroy an Instance?, and, to my surprise, when I tested my Question with a part, it still thought the Instance was existant, even though I called the Destroy method on it. I am puzzled on why that is, because, I thought the Destroy method truly Destroyed an Instance. My newest Question now is; Why doesn't the 'Destroy' method truly destroy an Instance? Here is the code I used in my experiment;

local Part = Instance.new("Part",game.Workspace)
if Part then
    print("Part Existant!") --Prints first time
end

wait(.1) --Waits .1 seconds

Part:Destroy() --Destroys the 'BasePart' instance

wait(.1) --Waits another .1 seconds

if Part then
    print("Part Existant!") --Prints again, thus, my question on why the 'Destroy' method does not truly destroy an Instance
end
0
I'm guessing it's because Part is a variable. They might be referring to the variable since the part is gone. The variable was made at the beginning of the script. So for that script ONLY the part will still be nil in a way. But if you were to make another script to see if part existed then it would not print anything. EzraNehemiah_TF2 3552 — 9y
0
I tested my theory and it seems to be correct. EzraNehemiah_TF2 3552 — 9y
1
I changed the 'if' statements to check if variable 'Part' is a 'BasePart' type instance just now, but, sadly, it has the same result. TheeDeathCaster 2368 — 9y

1 answer

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

It's because the semantics of Lua make it impossible to "delete" the variable Part by just calling something on it.

All values, excepting false and nil, are "truthy". That means they will pass an if.


Because of how Lua works,

local obj = {name = "cat"}
local same = obj

func(obj)

print(obj, same)

Now matter what func is in the top, I can guarantee that obj is still the same thing after (it still is same).

That's because while func can change the value of obj (for instance, it could change name to something else) it can't actually change obj as a variable.


The same is happening with :Destroy(). It doesn't eliminate the variable,* because you can't do that in Lua*.

Some other languages, e.g., C++, DO allow that sort of behavior. This is for convenience and performance, but usually results in very confusing code. That's why Lua does not allow it.


Part of the purpose of destroy is to mark the object for garbage collection. I'm not exactly sure what the full ramifications of this are.

The other part is simply to prevent it from coming back -- you can't set the parent on a :Destroy()ed object.

That may or may not help the garbage collector. Ultimately, the purpose may just be to keep good design, rather than for any performance reasons.

1
So basically my idea was pretty much correct, right? EzraNehemiah_TF2 3552 — 9y
2
Not quite. It is because it's in a variable, but it's not about which script or where it was defined. It's just a property of the language BlueTaslem 18071 — 9y
1
Okay, thanks for letting me know. EzraNehemiah_TF2 3552 — 9y
1
That actually makes allot of sense, because, it sounds a bit like the 'remove' method, because, I saw before that the 'remove' method only reverts the Childs Parent property to nil, but, the difference between 'remove' and 'Destroy' is that you can change the Childs Parent property when using 'remove'. TheeDeathCaster 2368 — 9y
View all comments (2 more)
1
Thanks for both of your help BlueTaslem, and Lord. :) I really appreciate you two taking your time to answer my question. :) TheeDeathCaster 2368 — 9y
0
Thanks :] Were searching for the answer. ErtyPL 129 — 2y
Ad

Answer this question