GetChildren won't get all the children

I’m trying to make a script that would change the transparency of all the parts inside of a group, and it seems to be working for the most part, but wouldn’t change the transparencies of all the parts.
This is the script


local debounce = false
function Click()
	local children = game.Workspace.MatMarkers1:GetChildren()
	if debounce == false then
		for i = 1, #children do
			local child = children[i]
			child.Transparency = 0.5
			print("hi")
			debounce = true
			end
	elseif debounce == true then
		for i = 1, #children do
			local child = children[i]
			child.Transparency = 1
			print("Hai")
			debounce = false
		end
		
	end	
	
end
script.Parent.ClickDetector.MouseClick:Connect(Click)
5 Likes

I would try using a for in pairs do loop which is just a for loop designed to loop through dictionaries, arrays, etc. (This is what GetChildren() returns). Using this will help lower clutter in the script.


If any errors were being shown in the output of your previous script it would help since the issue may not be with the code. But I would try the code I shown below.

local debounce = false

local function Click()
   for _,child in ipairs(game.Workspace.MatMarkers1:GetChildren()) do
      if debounce then
         child.Transparency = 0.5
      else
         child.Transparency = 1
      end
      debounce = not debounce
   end
end

script.Parent.ClickDetector.MouseClick:Connect(Click)

I tried the code, the issue actually ended up getting worse. There weren’t any errors in the output.

Hi @Catherine858 !

I’m not sure why it’s not changing the transparency of all the Children but you should add an if statement

if Child:IsA("BasePart") then end

so your code is less prone to errors, and you can also use ChildAdded to change the transparency of newly added Children (or catch ones that are late)


also use ipairs for arrays. GetChidren returns an Array


Seems like you have a weird Debounce boolean assignment going on, can you explain what you are trying to achieve so we can provide better code for your use case scenario?

Basically, I’m trying to make a part that when clicked, it would change the transparencies of all the parts within a specific group/folder. Clicking it once would turn it on, clicking it again would turn it off.


This is how it looks in-game after pressing the button.


Here are all the parts individually selected in the studio. I did double-check to make sure they were all in the same grouping.

It changes the transparency of all the parts, but it misses the last row of parts.

I’m on my iPad so you’ll have to type it out yourself but I’ll guide you.

   local transparency = isTransparent and 1 or .5 — flip transparency
   for _, child in ipairs(YourModel:GetChildren()) do
      if child:IsA("BasePart") then
         child.Transparency = transparency
      end
   end
   isTransparent = (not isTransparent) — flip isTransparent

You can also use this with YourModel.ChildAdded

Whoops! the code runs only once or might not at all, sorry about that let me fix it!

Fixed!

Hey, I wrote it out as this, is this the correct way to write it out?

function Click()
	local children = game.Workspace.MatMarkers1:GetChildren()
	if isTransparent then
		local transparency = isTransparent and .5
		for _, child in ipairs(MatMarkers1:GetChildren()) do
			if child:IsA("BasePart") then 
				child.Transparency = transparency
			end
		end
			isTransparent = (isTransparent)
			end
	if isTransparent then
		local transparency = isTransparent and 1
		for _, child in ipairs(MatMarkers1:GetChildren()) do
			if child:IsA("BasePart") then 
				child.Transparency = transparency
			end
		end
		isTransparent = (not isTransparent)
	end
		end

script.Parent.ClickDetector.MouseClick:Connect(Click)

No, that wouldn’t work quite well or at all,

You need to declare “isTransparent” as a local variable and you can replace my code with your click function, you should name it with something more meaningful like “toggleTransparency”

Sorry for asking so many questions :sweat_smile:

I’m a bit of a beginner in scripting, so I don’t really fully understand parts of the script you sent me, could you explain some parts?

Objects created on the server replicate to all the clients. Objects created on a client do not replicate to the server. you might wanna check where the objects are created. because if the getchildren() is serversided or on a different player’s client and the objects are created on your client, there will be issues.

you will be able to circumvent this by using remotevents to send instructions from the client to the server so it get’s created there.
Or you could put the GetChildren() in the a localscript so it runs clientsided.

I ended up fixing this by restarting studio, and using my original script. Not sure what happened, I’m assuming it must have been an issue with my computer.

2 Likes