Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to index field 'Parent' (a nil value), how can I fix that?

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.