Trying to understand why this NPC respawn script works

This is a respawn script is on the “Zombie” in the toolbox. This script works, but I"m trying to understand how. If you remove or destroy an object, does the script still run to the end? Also is the old model still in memory somewhere? How does the script know to spawn the backup if the old model with this script has been removed?

I have a separate script with a Humanoid.Died code on it, that gives credit to whoever killed the monster. It works, but every once in a while it doesn’t give credit because it cant find the humanoid. I think there is a conflict with this script. Thanks for your help in understanding.

z = script.Parent

backup = z:clone()

while true do
wait(5)
if z.Zombie.Health == 0 then
	z:Remove()
	wait(4)
	backup.Parent = game.Workspace
	backup.Head:MakeJoints()
	backup.Torso:MakeJoints()
end
if z == nil then
	wait(4)
	backup.Parent = game.Workspace
	backup.Head:MakeJoints()
	backup.Torso:MakeJoints()
end
end
1 Like

Remove doesn’t destroy an Instance, only sets his parent to nil. Whitch lets this script to work.

A disclaimer though: it is deprecated so it should, ideally, not be used in future coding practices.

3 Likes

What would be the replacement for Remove if it’s deprecated?

In this use case, running a loop and reparenting to nil would be optimal but ideally Instance:Destroy() is the preferred.

EDIT: Note that Destroy() will stop scripts from running if said script is parented to the destroyed instance.

2 Likes

Does :Destroy() set the parent of whatever you destroyed to nil just like what Remove does? or is it any different?

:Destroy sets the part’s parent to nil and doesn’t let it reparent the part.

Interesting so it works just like Remove right? so what’s the difference?

You can view the documentation here: Instance | Documentation - Roblox Creator Hub

It is Remove() with extra steps essentially.

1 Like

Ah ok, Well yes I realize this is depreciated. Destroy does something similar yes? Will a script like this be a memory leak, since each new spawn creates a new script? and the old scripts are still somewhere running?

Alright thank you for trying to explain it.

Remove sets a Instance’s parent to nil, and Destroy sets a Instance’s Parent to nil and you can’t reparent it.

1 Like

This can be a memory leak, but only if you have tons of zombies that are dying

1 Like

In order to answer all questions in this post, I will rewrite the code in the way that I believe it should be written:

local zombie = script.Parent;
local backupZombie = zombie:Clone();
local humanoid = zombie:FindFirstChild("Humanoid"); -- Change to WaitForChild() if client-sided

humanoid:GetPropertyChangedSignal("Health"):Connect(function(newValue)
    if newValue <= 0 then
        backup.Parent = workspace;
        backup:FindFirstChild("Head"):MakeJoints(); -- Never used MakeJoints before
        backup:FindFirstChild("Torso"):MakeJoints();
        
        zombie:Destroy(); -- Kills script and destroys instance
    end
end)

Also, you may be able to mix in your script here that detects who killed the zombie.

1 Like

He added a wait in his script, your script makes the zombie respawn instantly.

I am just curious to know why are you putting a semi colon ( ; ) after every line?

Thanks definitely something to work with to help me understand it better. I have no idea why make joints is needed. I don’t really get it.

He doesn’t need. I think he is better at JavaScript and uses the semi colon at every line.

This script worked, but had to make a few changes. Take the “newValue” out of the function and just leave that blank. In the if statement, just use humanoid.Health <= 0. I took out the make joints thing and it still works.

also the backup and backupZombie 2 different variables.

I realize you wrote it with fire speed. Just helping out so others can understand. Thanks for your help. I also got my kill detect script in there too.

Perfect! Glad to see I could help. Also, yes, I default to using the dot syntax versus what the function provides you in terms of newValue so I’m not sure why I used it that time.

@Nerkonl I put semicolons as a habit from working with other programming languages. They aren’t required in this use case.