Attempt to index nil with 'Humanoid'

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
2 Likes

script.Parent seems to not exist at all, where is it parented to?

1 Like

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.

1 Like

The Model of the zombie. script = script, Parent = Model

1 Like

In that case, change your if statement to this:

“if script.Parent:FindFirstChildOfClass(“Humanoid”) then”

If there’s still an error, there’s probably a parenting issue.

1 Like
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)
1 Like

That’s really odd. Even if there was a 10 seconds cycle, the script.Parent VANISHES into thin air. The mere possibility could even be that the zombie model flung or fell into the void.

1 Like

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.

1 Like

I got the turn line 5 into a variable part. The only thing is how would i copy and paste the model from server storage into workspace?

1 Like

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

1 Like

do I need to have both or just 1 model?

You’ll probably need both, one in workspace to start things off, and one in ServerStorage for the respawn.

1 Like

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.

1 Like

What is the name of the object of the “Humanoid” class?

1 Like

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.

1 Like

The model could have fallen into the void or destroyed by some external script.

1 Like

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

1 Like
local zHumanoid=game.Workspace.Zombie:FindFirstChild("Humanoid")
zHumanoid.Died:Connect(function()
	game:GetService(“ServerStorage”):FindFirstChild(“Zombie”):Clone().Parent = workspace
end)

would that work? I put the script into ServerScriptService

1 Like

Test it out. Tell us if there are any errors in the output.

1 Like

The model does not have any external respawn script

1 Like