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