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

Attempt to call a nil value when using GetChildren()?

Asked by 2 years ago

Ok I have an issue I want to find the children's children of a folder so I can change their properites / load them in.

This is my script:

function onTouched(part)
    wait (0.5)
    if part.Parent:findFirstChild("Humanoid")~=nil then
        local map = script.Parent.Parent.Parent
        local waves = map.Waves
        print (waves)
        local Wave1 = waves.Wave1:GetChildren():GetChildren()
        print (Wave1)
        Wave1.Anchored = false
        Wave1.Transparency = 0
end
end

script.Parent.Touched:connect(onTouched)

So when someone touches this part it should make the things visible and make them not anchored.

Instead it just gives me this error: Workspace.UpgradeOpen.Part.Script:7: attempt to call a nil value

it does print "Waves" from line 6 so it cant be the issue that it doesnt find the folder.

Any help will be appreciated!

2 answers

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

you can't get children twice, i think you want to get the childrens after getting the childrens,,,,, well you can't get childrens on table (because get children returns table), so you should need to use ipairs loop i think

for _, instance in pairs(waves:FindFirstChild("Wave1"):GetChildren()) do -- get wave1 childrens
    for _, instancewave in pairs(instance:GetChildren()) do -- loop through instance childrensĀ (if there is no children, then it won't print anything
        print(instancewave.Name) -- you might need this one??
        instancewave.Anchored = false -- or unanchor it, make lag lol
    end

    print(instance.Name) -- or this one
    instance.Anchored = false -- if it's already false then you can still make it false
end

another way is to get descenants, get descenants are different than get children, because it get children, but if the children have childrens, then it gets the instance's children, until everything is clear

in coolfds solution i won't copy and paste because that do nothing

0
AAA WHY I SAY CHILDRENS SO MANY TIME SUPER GRAMMAR MISTAKE Xapelize 2658 — 2y
0
Ok but how can I getdecendants twice? So i get decendants and it returns all of the model but I want to edit the children of the model not the model itself epicnmac 63 — 2y
0
NO, you dont need to get decendants twice, get decendants already returns everything even wave's children's children, if you confused then use 1st way Xapelize 2658 — 2y
Ad
Log in to vote
1
Answered by 2 years ago

It might be because your object is nil or your defining the name as a nil object. Either you could do is if thats not the case. And 2 GetChildren() doesn't work. Use, GetDescendants() instead.

local wave1 = waves:FindFirstChild("Wave1"):GetDescendants()

Hopefully this helped.

0
Can I do getDescendants twice? epicnmac 63 — 2y
0
Nope. cooIfds 5 — 2y

Answer this question