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 6 years ago

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.

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

3 answers

Log in to vote
0
Answered by 6 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.

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!

Ad
Log in to vote
0
Answered by 6 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
6 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.

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)
0
the door gets removed instantly when you play the game, anything else? ShutokouBattle 227 — 6y

Answer this question