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.
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)