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
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?
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.
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.