Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

Log in to vote
0
Answered by 7 years ago

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

Ad
Log in to vote
0
Answered by
Tomstah 401 Moderation Voter
7 years ago

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.

1
lol you didn't even look through it good. You're gonna need hit.Parent:FindFirstChild(), not script.Parent. RubenKan 3615 — 7y
0
Oh damnit... yep, my bad. My fault for assuming there's only ever one problem xD Tomstah 401 — 7y

Answer this question