It SOMETIMES works fine, but sometimes the script just completely broken, and gives me an error on line 13: attempt to index field 'Parent' (a nil value) What is wrong and how can I fix this?
name="Human" local t = script.Parent.Config.RespawnTime.Value robo=script.Parent:clone() while true do wait(t) if script.Parent.Human.Health<1 then name=robo:clone() name.Parent=script.Parent.Parent name:makeJoints() script.Parent:remove() end end
What is wrong is sometimes the while true do could be too late, run after the humanoid model has been despawned.
Instead of a while true do use the Died event on the Humanoid, so the code runs instantly
local t = script.Parent.Config.RespawnTime.Value local humanoid = script.Parent:FindFirstChildOfClass("Humanoid") --Finds a humanoid local clone = script.Parent:Clone() --our clone, for respawning humanoid.Died:wait() --waits until the humanoid dies clone.Parent = workspace clone:MakeJoints()
No need to destroy script.Parent because it does it automatically.