while true do
wait()
if script.Parent.Value <0 then
script.Parent.Value = 0
wait(.31)
script.Parent.Parent:Destroy()
end
end
-- lol
this is a easy script but how do i fix this attempt to index field 'Parent' (a nil value)?
1 answer
Well, if you destroyed the script's parent on the first iteration, the parent will not be existent anymore. Nil is the same thing as nothing, null, nonexistant, void.
If it manages to go through the if condition twice, it will try to destroy something that is not there. So, it will attempt to destroy nothing.
And, since Lua hates when something goes wrong (unlike html), it will error and cry under the sheets. So, you want to prevent that, right?
To prevent it, just check if the script has a parent, and if the script's parent's parent is available.
TL;DR, you should check if the scripts parent (and the script's parent parent) isn't nil.
P.S. To save yourself another line, instead of doing
while true do
wait()
print'green eggs in ham still hates sam i am'
end;
you can do:
while wait() do
print'green eggs in ham still hates sam i am'
end;
2 comments
Don't encourage bad practices. while wait() do is bad practice
how is it bad practice?
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
On line three maybe it needs to be ' if script.Parent.Value <<0 then '
it did not work, its line 6 that give the error
Where is this script?