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

Another "index field 'Parent' (a nil value)"... sorry. How to stop it firing for non-characters?

Asked by 5 years ago

Hi all, I'm entering the debug phase of my first build prior to an alpha release and I need a bit of guidance. I've looked around at all the other instances of a similar error but if someone more knowledgable than I could explain how to prevent it with regards to my scenario it would really help my learning going forward.

The context: I have a baseplate that I've turned into lava, in that I have a super basic script to kill characters that make contact with it.

function onTouched(part)
        local h = part.Parent:findFirstChild("Humanoid")
        if h ~= nil then
                 h.Health = 0
        end
end
script.Parent.Touched:connect(onTouched)

However, in testing the game I get the following error every couple of seconds: Workspace.Stages.1. lava.Baseplate.Script:3: attempt to index field 'Parent' (a nil value) This is due to a series of boulders that roll down a slope and make contact with the baseplate before being removed from the game. I've tried a few of the solutions I've read to no avail (adding a wait() or simply defining the Parent to stop it being nil).

Just from writing this I realised it was the boulders causing the problem, which doesn't break the game, just floods the console making further debugging difficult. Any advice to help advance my learning would be greatly appreciated. Thanks

0
That can happen when a bullet hits a part; the bullet changes it's parent to `nil` (or when it's destroyed), or if something else causes that as well. The solution to check whether it's nil or not via `if part.Parent then`. TheeDeathCaster 2368 — 5y
0
Thanks for the comment, I tried inserting a if part.Parent then which makes sense as a potential solution but then realised that the parts causing the issue were more than just the cloned boulders, there are moving platforms as well GrumpyTheDaddy 17 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You could try this, although I don't see why you're getting that error.

I reformed the way you made the function cuz this is a cleaner way of writing it. Also, it's easier to do 'if h then' as opposed to 'if h ~= nil then' as they mean the same thing

script.Parent.Touched:connect(function(t)
    if t.Parent then
        local h = t.Parent:FindFirstChild("Humanoid") 
        if h then
            h.Health = 0
        end
    end
end)
0
I'm still very much at the 'copy/paste then hit it with a hammer' stage of my learning but I can see how that is cleaner... AND it seems to have fixed my issue, presumably because if the function doesn't find a humanoid it just gives up. Thanks, TIL :) GrumpyTheDaddy 17 — 5y
0
Np RequiredModule 38 — 5y
0
He's getting it because he's trying to assign an Instance of which is a presumed descendant of anything that touches the Lava. Though this is untrue, so sometimes it will say it's not there as it's only anticipated for the Character. Ziffixture 6913 — 5y
0
He's getting it because he's trying to assign an Instance of which is a presumed descendant of anything that touches the Lava. Though this is untrue, so sometimes it will say it's not there as it's only anticipated for the Character. Ziffixture 6913 — 5y
Ad

Answer this question