[VERY New to Lua] Utilizing Getchildren

Hey! I’m relatively new to scripting [I’ve just learned extreme basics over scanning through other’s scripts, and I’m mainly a builder, so it’s a bit foreign to me.]
I’m currently trying to learn lua, and I’m working on a script that can open a door by making the parts transparent.
The script here is currently working, and I’m not having issues with it- However, I’m looking for a way to save time instead of constantly listing all parts.
I know there’s a thing called Getchildren, however I’m not completely sure how to use it. (Again, complete and total noobie.)

Script
local isOn = true

function on()
isOn = true
script.Parent.door1.CanCollide = true
script.Parent.door1.Transparency = 0
script.Parent.handle3.CanCollide = true
script.Parent.handle3.Transparency = 0
script.Parent.handle4.CanCollide = true
script.Parent.handle4.Transparency = 0
script.Parent.poster1.CanCollide = true
script.Parent.poster1.Transparency = 0
script.Parent.poster1.decal1.Transparency = 0
end

function off()
isOn = false
script.Parent.door1.CanCollide = false
script.Parent.door1.Transparency = 1
script.Parent.handle3.CanCollide = false
script.Parent.handle3.Transparency = 1
script.Parent.handle4.CanCollide = false
script.Parent.handle4.Transparency = 1
script.Parent.poster1.CanCollide = false
script.Parent.poster1.Transparency = 1
script.Parent.poster1.decal1.Transparency = 1
end

function onClicked()

if isOn == true then off() else on() end

end

script.Parent.door1.ClickDetector.MouseClick:connect(onClicked)

on()

Really sorry about this !! I don’t want to be a bother at all, so I’m kinda scared to ask this because I know the answer’s going to be extremely simple, and I’ll probably look a bit dumb.
Any help would be appriciated though !! :upside_down_face:

GetChildren returns all children of a instance in a table, to utilize it do something like this:

local children = Model:GetChildren()

for i,v in pairs(children) do
 --your code, v is the children
end

For more information about it, check the Developer Article out!
https://developer.roblox.com/en-us/api-reference/function/Instance/GetChildren

2 Likes

Ah, thank you !! I’ve mostly used the devforums on mobile, and didn’t even see that the API reference existed. I really appreciate that !! :>

1 Like

Also just a side note for your future scripting endeavors:

GetChildren() gets the immediate children of whatever instance you’re looking at it. For example - game.Workspace.Frame:GetChildren() would allow me to reference all of these: image

However, you can also use GetDescendants() to essentially get all of the children of every child within whatever instance you use GetDescendants with. For example - game.Workspace.Frame:GetDescendants() would allow me to reference all of these without having to use GetChildren on each respective instance within my big parent/ancestor Frame instance.
image

Hope that was helpful as well!

4 Likes

Quick formatting tip:

Use three back ticks ``` to start and end a code block to help make your script more readable. For example:

```
function hello()
print( ‘hello’ )
end
```

becomes

function hello()
    print( 'hello' )
end

Hope that helps if you ever need any future support here :slightly_smiling_face:

1 Like