Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

attempt to index nil with 'WaitForChild'?

Asked by 5 years ago

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

01local char = script.Parent
02local hum = char:WaitForChild("Humanoid")
03local backUpChar = char:Clone()
04backUpChar:WaitForChild("DeathAnims"):Destroy()
05 
06hum.Died:Connect(function()
07    local deathPos = char.HumanoidRootPart.Position
08    local deathOri = char.HumanoidRootPart.Orientation
09    local bac = backUpChar:Clone()
10    local destme = char
11    script.Parent = bac.Humanoid
12    destme:Destroy()
13    bac.Parent = workspace
14    bac.HumanoidRootPart.Position = deathPos
15    bac.HumanoidRootPart.Orientation = deathOri
View all 30 lines...

3 answers

Log in to vote
6
Answered by
ArtBlart 533 Moderation Voter
5 years ago

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.

0
Good answer DeceptiveCaster 3761 — 5y
0
Huh. Never knew what the property really did. Thanks! Utter_Incompetence 856 — 5y
Ad
Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
5 years ago

I recommend using Find First Child

1backUpChar:FindFirstChild("DeathAnims"):Destroy()
0
If it doesn't exist then it'll give an unclear error of "attempt to index nil" hiimgoodpack 2009 — 5y
Log in to vote
0
Answered by 5 years ago

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

1WaitForChild("Name To Search For", 10)

Answer this question