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 4 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

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

Log in to vote
6
Answered by
ArtBlart 533 Moderation Voter
4 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 — 4y
0
Huh. Never knew what the property really did. Thanks! Utter_Incompetence 856 — 4y
Ad
Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago

I recommend using Find First Child


backUpChar:FindFirstChild("DeathAnims"):Destroy()
0
If it doesn't exist then it'll give an unclear error of "attempt to index nil" hiimgoodpack 2009 — 4y
Log in to vote
0
Answered by 4 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

WaitForChild("Name To Search For", 10)

Answer this question