I am trying to fix this error but I can not figure how to debug it. Workspace.Zombie.Script:7: attempt to index nil with 'Humanoid’
name="Humanoid"
robo=script.Parent:clone()
while true do
wait(10)
if script.Parent.Humanoid.Health<1 then
robot=robo:clone()
robot.Parent=script.Parent.Parent
robot:makeJoints()
script.Parent:remove()
end
end
This code has some things you could improve on, aside from the bug;
Turn the while do loop into a function with a Died:Connect() event.
Replace the “remove()” with Destroy()
Clarify values a bit more, remove unneeded variables, make them local if required.
Now as far as the bug goes, it could be an issue with the script’s parent. Make sure it’s in a (with a humanoid) model and not a part inside the model.
script.Parent.Humanoid.Died:Connect(function()
local robo=script.Parent:clone()
wait(10)
if script.Parent.Humanoid.Health<1 then
robo:clone()
robo.Parent=script.Parent.Parent
robo:makeJoints()
script.Parent:Destroy()
end
end)
That’s really odd. Even if there was a 10 seconds cycle, the script.ParentVANISHES into thin air. The mere possibility could even be that the zombie model flung or fell into the void.
You have 2 clones, one defined at line 2 and another defined at line 5.
I’d say keep the one at line 5 and turn it into a variable like you did with the one at line 2.
Also, even better, create a duplicate of your model, put it into ServerStorage, then when your model’s Humanoid dies, copy that model into the workspace.
Use Ctrl-D on the model you want to duplicate, put the duplicate (or the original, both work) into ServerStorage, then have a script that’ll fire when the one in workspace dies, optionally wait a bit, then spawn a new one by cloning the one that’s in ServerStorage
Code Sample:
function X()
game:GetService(“ServerStorage”):FindFirstChild(“ModelName”):Clone().Parent = workspace
end
You removed the script’s Parent the first time the loop is run, so script.Parent = nil when the loop runs for a second time. Thus, it errors since Humanoid is not a member of nil.
I don’t think this is related to the issue but you are detecting the script.Parent (zombie)'s Humanoid Health. Why not robo’s humanoid health? Also I don’t see any code deleting the script.Parent so this is odd.
True, if the zombie have a script that respawns itself after 5 second, then it might be the issue, since the new script is using Humanoid.Died function