Scripting Helpers is winding down operations and is now read-only. More info→
← Blog Home

Do you have zombies in your code?

Disregarding how cool it would be to have actual zombies running through your code and stomping on your errors - we don't actually mean the typical zombie you're thinking of.

We're using zombie to refer data left over from incorrectly removing something from your game. This could happen from ways like setting the parent of something to nil, or using that rusty, old remove function.

-- bad ways
Workspace.Part.Parent = nil
Workspace.Parent:Remove()

Let's be quick about it. What is the best way to make sure something is not lingering in the graveyard of data that you can't really see in the Workspace - or anywhere else in your game? Using the :Destroy() method.

Workspace.Part:Destroy()

Why is that? First off, simply setting the Parent property to nil will not remove the rest of the data that the part has allocated. This is why you can re-parent the part back to an existing object (like the Workspace) and it will still have all its original properties. The same goes for :Remove(). However, remove will go through all the children and descendants - setting all of their Parent properties to nil.

Notice how, again, it doesn't set all other properties to nil - and the parts are never collected via the garbage collection in Lua. This means they still exist someone as zombie parts waiting to come up and eventually lag your games.

However, there are currently scripts out there that use the :Remove() method and expect the zombie parts. They use these to their advantage. The most common sort of script that does this is a tycoon buy-button. However, this is also the reason for buy-buttons sometimes not working. Occasionally, the garbage collector will become throttled and collect the nil-parented parts used by buy-buttons. Use a different method in this case.

Just remember to always use :Destroy() when removing ROBLOX objects. We usually don't want things to rise from the dead and make everything explode.

Posted in Scripting Tips

Commentary

Leave a Comment

nate890 says: February 17, 2014
Zombies? No! Not zombies!
Mazux says: February 19, 2014
Oh no, zombies.
adark says: February 21, 2014
I actually have a question about this. When the garbage collector is throttled, does that delete all objects parented to nil directly, or only those Remove() has been called on? I'm assuming the former, but I thought I'd ask anyway.
FiredDusk says: May 15, 2017
2017
AstrealDev says: May 18, 2017
5/8/2017
WideSteal321 says: January 3, 2019
2019
User#28017 says: June 18, 2019
I've never heard of the Remove() function lol. I've always used Destroy() when taking something out of my game.