Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

this is a easy script but how do i fix this attempt to index field 'Parent' (a nil value)?

while true do
	wait()
if script.Parent.Value <0  then
	script.Parent.Value = 0
	wait(.31)
	script.Parent.Parent:Destroy()
	end
end 
-- lol

3 comments

0

On line three maybe it needs to be ' if script.Parent.Value <<0 then '

0

it did not work, its line 6 that give the error

0

Where is this script?

1 answer

✓ Accepted 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

0

Don't encourage bad practices. while wait() do is bad practice

0

how is it bad practice?

This discussion is preserved as it was. New answers, comments, and votes are closed.