Some guy told me that it tries to destroy the parent twice, which causes this error.
1 | local hum = script.Parent.Humanoid |
2 |
3 | script.Parent.Humanoid.MaxHealth = Random.new( 50 , 100 , 120 , 75 , 60 , 65 ) |
4 | script.Parent.Humanoid.Health = script.Parent.Humanoid.MaxHealth |
5 |
6 | while wait() do |
7 | if hum.Health = = 0 then |
8 | 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.
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.
01 | local hum = script.Parent:WaitForChild( "Humanoid" ) |
02 |
03 | function 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 |
09 | end |
10 |
11 | hum.MaxHealth = RandomNumb( 50 , 60 , 65 , 75 , 100 , 120 ) |
12 | hum.Health = script.Parent.Humanoid.MaxHealth |
13 |
14 | hum.Changed:Connect( function (property) |
15 | if hum.Health < = 0 then |
16 | script.Parent:Destroy() |
17 | end |
18 | 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.
1 | local hum = script.Parent.Humanoid |
2 |
3 | hum.MaxHealth = Random.new( 50 , 100 , 120 , 75 , 60 , 65 ) |
4 | hum.Health = script.Parent.Humanoid.MaxHealth |
5 |
6 | hum.Died:Connect( function () -- that is an event. |
7 | script.Parent:Destroy() |
8 | end ) |