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

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

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

✓ Accepted answer

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

3 comments

0

AAA WHY I SAY CHILDRENS SO MANY TIME SUPER GRAMMAR MISTAKE

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

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

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.

2 comments

0

Can I do getDescendants twice?

0

Nope.

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