How to :Destroy() Humanoids without errors?

For some reason this script kinda works but it creates “index nil with :FindFirstChild” error every time

This script kills humanoids with a “Plr” Instance in it, if not it will just delete it instead
This is to kill off ‘Invincible’ enemies by pushing them off into the void

( Ex: This enemy when killed it turns into a pile of pushable mass for a while, before it comes back alive the ‘Player’ have to push it off into the void to kill it. The void only sets humanoid health to 0 so the enemy will still turn into its alive form then die and repeat because the pushable mass turns back into the enemy when killed also )

script.Parent.Touched:Connect(function(Hit)
	if Hit and Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent:FindFirstChild("Plr") then
		Hit.Parent.Humanoid.Health = 0
	else
		Hit.Parent:Destroy()
		
end
end)

I’m very new to scripting so I really don’t know how to explain it but any thing helps

1 Like

You aren’t checking if Hit.Parent exists. Add it to your first if check and it should work :slight_smile:

1 Like

I put in “and Hit.Parent” but it’s not working

local debounce = false
script.Parent.Touched:Connect(function(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid") -- Character.Humanoid
    local plr = hit.Parent:FindFirstChild("Plr")
    if humanoid and debounce == false and plr then --If humanoid exist and debounce is false
       debounce = true
       humanoid.Health = 0
       task.wait(3)
       debounce = false
    end
end)
1 Like

I’m bit confused here. Does the Plr is a boolean value or something? Then, does the “Plr” value is parented to character or character.Humanoid?

It is a “String Value” named to “Plr” so I can use :FindFirstChild()

if plr.Value == humanoid.Name then
   -- refer humanoid.Name and check if it is equal to StringValue.Value, plr.Value
end

I tried to test configure it to :Destroy but I still got the “index nil with :FindFirstChild” error even though the object was clearly there

1 Like

"index nil with :FindFirstChild error " refer to an FindFirstChild function that are being called from nil instace(doesn’t exist).
Try using WaitForChild() and naming thing properly.Plus, try to make sure the parent and child path is correct.

1 Like