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), how can I fix that?

Asked by
0msh 333 Moderation Voter
6 years ago

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

1 answer

Log in to vote
0
Answered by 6 years ago

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.

Ad

Answer this question