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

attempt to index nil with 'WaitForChild'?

I'm making a death animations script and upon testing, the error

Workspace.Character.DeathAnims:4:attempt to index nil with 'WaitForChild'

appears in the output

local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local backUpChar = char:Clone()
backUpChar:WaitForChild("DeathAnims"):Destroy()

hum.Died:Connect(function()
	local deathPos = char.HumanoidRootPart.Position
	local deathOri = char.HumanoidRootPart.Orientation
	local bac = backUpChar:Clone()
	local destme = char
	script.Parent = bac.Humanoid
	destme:Destroy()
	bac.Parent = workspace
	bac.HumanoidRootPart.Position = deathPos
	bac.HumanoidRootPart.Orientation = deathOri
	bac:MakeJoints()
	hum = bac.Humanoid
	hum.HealthDisplayDistance = 0
	hum.MaxHealth = math.huge
	hum.Health = math.huge
	local death_Anim = Instance.new("Animation")
	death_Anim.AnimationId = 'rbxassetid://4643698669'
	local track = hum:LoadAnimation(death_Anim)
	--script.Parent.Parent.HumanoidRootPart.Anchored = true
	track.Priority = Enum.AnimationPriority.Action
	track:Play()
	wait(0.66)
	hum.Health = 0
	script:Destroy()
end)

3 answers

✓ Accepted answer

Ooh this is good example of Roblox properties messing with people.

Characters automatically come with their Archivable property off, which means it cannot be cloned without turning the property back on first. All you need to do to fix up your script is: before your WaitForChild line which errors, set char.Archivable to true.

If you wanna know more about the property there is more on the dev wiki here.

2 comments

0

Good answer

0

Huh. Never knew what the property really did. Thanks!

I recommend using Find First Child


	backUpChar:FindFirstChild("DeathAnims"):Destroy()

1 comment

0

If it doesn't exist then it'll give an unclear error of "attempt to index nil"

I have had this same type of problem, I did some research and found out that "WaitForChild()" has a "TimeOut" feature meaning you can set how long you want is to wait for:

the "10" represents however long you want it

WaitForChild("Name To Search For", 10)

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