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

"attempt to index field "Parent" (a nil value)"?

Some guy told me that it tries to destroy the parent twice, which causes this error.

local hum = script.Parent.Humanoid

script.Parent.Humanoid.MaxHealth = Random.new(50,100,120,75,60,65)
script.Parent.Humanoid.Health = script.Parent.Humanoid.MaxHealth

while wait() do
if hum.Health == 0 then
script.Parent:Destroy()

I'm trying to make a script that deletes a door when it's health reaches 0. But when the game starts up, it gives the question's title.(the error) I've tried doing it in two scripts (the 6th line loop) but no go. This is a ServerScript.

3 comments

0

Is there even an end to the while loop?

0

lol, I coded line 6 while making the question, and forgot the end... oops.

0

With "Random.new" you trying to get a random number from the set of numbers specified next to it?

3 answers

✓ Accepted answer

Here's the script for your destroyable door, I basically replaced .Random with a function which returns a random number from the set of numbers given (look at line 11). Also changed your "while wait() do" loop for a .Changed event, much more effective and consumes a lot less memory from your game.

local hum = script.Parent:WaitForChild("Humanoid")

function RandomNumb(...)
	local tuple = {...}
	local number = math.random(#tuple)
	for i,v in ipairs(tuple) do
		if i == number then return v end
	end
end

hum.MaxHealth = RandomNumb(50, 60, 65, 75, 100, 120)
hum.Health = script.Parent.Humanoid.MaxHealth

hum.Changed:Connect(function(property)
	if hum.Health <= 0 then
		script.Parent:Destroy()
	end
end)

Just in case, the door I tested this out and its hirerarchy are avaible to see in this image.

Hope this helped. Have a good day!

try putting a wait (10) after script.parent:destroy() not 100% sure but think it will fix

After you deleted the script and its parent, it still functions. I think a while loop will always run even when the script is gone. Use the Died event.

local hum = script.Parent.Humanoid

hum.MaxHealth = Random.new(50,100,120,75,60,65)
hum.Health = script.Parent.Humanoid.MaxHealth

hum.Died:Connect(function() -- that is an event.
	script.Parent:Destroy()
end)

1 comment

0

the door gets removed instantly when you play the game, anything else?

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