Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago

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

1local hum = script.Parent.Humanoid
2 
3script.Parent.Humanoid.MaxHealth = Random.new(50,100,120,75,60,65)
4script.Parent.Humanoid.Health = script.Parent.Humanoid.MaxHealth
5 
6while wait() do
7if hum.Health == 0 then
8script.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.

0
Is there even an end to the while loop? frostysubatomiczero 51 — 7y
0
lol, I coded line 6 while making the question, and forgot the end... oops. ShutokouBattle 227 — 7y
0
With "Random.new" you trying to get a random number from the set of numbers specified next to it? Le_Teapots 913 — 7y

3 answers

Log in to vote
0
Answered by 7 years ago

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.

01local hum = script.Parent:WaitForChild("Humanoid")
02 
03function RandomNumb(...)
04    local tuple = {...}
05    local number = math.random(#tuple)
06    for i,v in ipairs(tuple) do
07        if i == number then return v end
08    end
09end
10 
11hum.MaxHealth = RandomNumb(50, 60, 65, 75, 100, 120)
12hum.Health = script.Parent.Humanoid.MaxHealth
13 
14hum.Changed:Connect(function(property)
15    if hum.Health <= 0 then
16        script.Parent:Destroy()
17    end
18end)

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!

Ad
Log in to vote
0
Answered by 7 years ago

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

Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
7 years ago

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.

1local hum = script.Parent.Humanoid
2 
3hum.MaxHealth = Random.new(50,100,120,75,60,65)
4hum.Health = script.Parent.Humanoid.MaxHealth
5 
6hum.Died:Connect(function() -- that is an event.
7    script.Parent:Destroy()
8end)
0
the door gets removed instantly when you play the game, anything else? ShutokouBattle 227 — 7y

Answer this question