I have a script inside a part, and when you touch the part, you're supposed to die, but when it touches the grass, it removes itself, and when it touches a part, it sets anchored to false. But when it touches you, a error comes up
21:33:27.795 - Workspace.GameRock.RockS:2: attempt to index field 'Parent' (a nil value)
Script:
script.Parent.Touched:connect(function(part)
local h = part.Parent:findFirstChild("Humanoid")
local g = part.Parent:findFirstChild("Grass")
local p = part.Parent:findFirstChild("Part")
local r = part.Parent:findFirstChild("Rock")
if h then
h.Health = 0
elseif g or r then
script.Parent:Destroy()
elseif p then
p.Anchored = false
end
end)
EDIT:
Sometimes the error happens, and other times it doesn't happen. but more often than not.

1 comment
Usually, when I think of this type of Error, I think of the code 'if not hit.Parent then return end' to prevent something like this from happening, although, I don't suggest doing this, just a thought. :)