How do I select the children in a model?

So, I’m making a keycard system, and I want to select the children in the model, as Model doesn’t have Transparency or CanCollide. So, I tried to make a system to select the children, but it failed. I need to have the door as a model, as in unions, the decals, and materials don’t save.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "Keycard" then
		workspace.Door1:getChildren("CanCollide", false)
		workspace.Door1:getChildren("Transparency", 1)
		wait(1)
		workspace.Door1:getChildren("CanCollide", true)
		workspace.Door1:getChildren("Transparency", 0)
	end
end)
1 Like

:GetChildren() is a function that returns all children of an instance into a table.
Anyways, to get the values inside a table, you have to use pairs loop.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "Keycard" then
        for _,v in pairs(workspace.Door1:GetChildren()) do
		  if v:IsA("BasePart") then
         --makes your part invisible
         --remember, "v" is the part
         --v.Transparency = 1
         --v.CanCollide = false
         --wait(1)
         --v.Transparency = 0
         --v.CanCollide = true
          end
        end
	end
end)

Anyways, the solution bacon guy give is the right one

1 Like

Okay! I’ll use your script like you said, and see if it works. I’ll remember to mark it as the solution if it works.

1 Like

I’ll also try what you said! Maybe it could work too.

This happened, I’m not sure what is going wrong in the script. It’s really weird.
https://gyazo.com/60b55b9ed89f9f752bb8bacce8c735cf
The gif may be a bit laggy, sorry for that! I’ll try Nerk’s script and see if the same thing happens.

If you have models inside your models, you’ll need to replace GetChildren with GetDescendants.

1 Like

@Nerkonl I’m having a problem with where random parts turn transparent, and other parts become non-collidable. It’s really weird.

The same thing that happened here happened again, I’ll try and see if I have anything in the model that could be causing issues.

I don’t believe I put any models in the Door, but I did put a decal, could that cause the issue?
I’m still pretty new to lua, so I don’t really know.

You will need to do for loop, where you make them Transprent and not Collidable

Then outside of the loop, do wait(1) and then loop with making then normal again.

script.Parent.Touched:Connect(function(hit)
	if hit.Parent.Name == "Keycard" then
        for _,v in pairs(workspace.Door1:GetChildren()) do
		  if v:IsA("BasePart") then
         --makes your part invisible
         --remember, "v" is the part
         v.Transparency = 1
         v.CanCollide = false
           end
         end
         wait(1)
          for _,v in pairs(workspace.Door1:GetChildren()) do
		  if v:IsA("BasePart") then
         --makes your part invisible
         --remember, "v" is the part
         v.Transparency = 0
         v.CanCollide = true
           end
        end
	end
end)

Sorry for anything weird, written on phone.

4 Likes

litteraly what this guy aboves says
if you put wait(1) inside the loop, it will make the the parts go invisible and visible one by one,
so you have to make a loop to where it becomes invisible
and wait(1)
and make another loop where it becomes visible again
Also,

@Barothoth I don’t quite understand the different, can you tell me so I can get some extra information uwu

1 Like

I think that GetDescendants is basically like GetChildren, but it selects more parts in the model? I think that’s what he means. That’s at least what I think he said.

I also have a decal in this, I guess I’ll just delete it as it’s awkwardly showing up.
https://gyazo.com/77454e0ac7873ef6d3701b06e0ce41ab
Thank you for the help!

GetDescendants is a recursive function where it checks all children in the object, and all the children of those children, and all the children of those children, and so forth until their is nothing left. If we are still checking for base part, that means when are selecting every part in the model no matter how complex the model is.

2 Likes

honestly, been years and never learned that simple and useful info, thank you very much
only if I can make urs as an solution

2 Likes

If only I could make all of your responses solutions because you guys really helped me out! I was thinking on just making the door a singular part and it’d make it look bland and uninteresting.