Hello everyone, so I’m working on a script where once the NPC dies the parts will disappear after 5 seconds after death then re-appears 100 seconds after death. Can someone help me out, thanks.
name="Humanoid"
robo=script.Parent:Clone()
while true do
wait(5)
if z.NPCTest.Health == 0 then
z:Remove()
wait(4)
backup.Parent = game.Workspace
backup.Head:MakeJoints()
backup.Torso:MakeJoints()
end
while true do
wait(100)
if script.Parent.Humanoid.Health<1 then
robo=robo:Clone()
robo.Parent=script.Parent.Parent
robo:makeJoints()
script.Parent:remove()
end
end
First of all, remove() is depreciated, so it should not be used.
Secondly , you might have just left it out of your post, but I don’t think you have your “z” variable defined. I am assuming this is the CPU’s character model.
And thirdly, the basic code is good, but the structure is a tad wrong.
The second while loop you never has a way to exit the loop, so the first while loop will never take action. I don’t see why you have that while loop at all honestly.
Example code, created on mobile and is just to demonstrate:
better structure(is not tested and is just an example)
name="Humanoid"
robo=script.Parent:Clone()
while true do
wait(5)
if z.NPCTest.Health == 0 then
died = true
z:Destroy() --destroy() instead of remove()
wait(4)
backup.Parent = game.Workspace
backup.Head:MakeJoints()
backup.Torso:MakeJoints()
wait(100)
if script.Parent.Humanoid.Health<1 then
robo=robo:Clone()
robo.Parent=script.Parent.Parent
robo:makeJoints()
script.Parent:Destroy()
end
end
end
This same task could also be completed in a more optimal format, by creating a OnDeath function that fires when the CPU dies. This is just my suggestion for a approach that might work a bit better.
optimized version(is not tested and is just an example)
name="Humanoid"
robo=script.Parent:Clone()
z.NPCTest.Died:Connect(function() --connecting the "Died" event of the NPC's Humanoid to a function
z:Destroy() --destroy() instead of remove()
wait(4)
backup.Parent = game.Workspace
backup.Head:MakeJoints()
backup.Torso:MakeJoints()
wait(100)
if script.Parent.Humanoid.Health<1 then
robo=robo:Clone()
robo.Parent=script.Parent.Parent
robo:makeJoints()
script.Parent:Destroy()
end
end)