Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My killbrick doesn't kill when touched and gives errors, what did I do wrong?

The killbrick gives the error attempt to index local 'humanoid' (a nil value), what did I do wrong?

function onTouch(hit)
	local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
		humanoid.Health = 0
	end
script.Parent.Touched:Connect(onTouch)

2 answers

✓ Accepted answer

Here it is correctly lol I saw you rushed so ill give it here:

function onTouch(hit)
    local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid then --This will only run the conditional if humanoid exists.
            humanoid.Health = 0
        end
    end
script.Parent.Touched:Connect(onTouch)

ther, now that works

What's happening is it's hitting some random block and trying to find a humanoid.

After this, FindFirstChild returns nil. Then you tried to index "Health" for nil. 'Causing the error.

function onTouch(hit)
    local humanoid = script.Parent.Parent:FindFirstChild("Humanoid")
		if humanoid then --This will only run the conditional if humanoid exists.
			humanoid.Health = 0
		end
    end
script.Parent.Touched:Connect(onTouch)

Also, I think you put one too many ".Parent"'s as well. So you might want to look into that.

2 comments

1

lol you didn't even look through it good. You're gonna need hit.Parent:FindFirstChild(), not script.Parent.

0

Oh damnit... yep, my bad. My fault for assuming there's only ever one problem xD

This discussion is preserved as it was. New answers, comments, and votes are closed.