How to get children for multiple decals inside the same part?

So I have this script:
image

And the “Part1” which has 7 decals inside of it:
image

I wanted to get children of that Part1 to get all the decals and make their transparency = 1 inside the function in the script all at once, but I have no idea how. (instead of manually typing Part1.Decal.Transparency = 1 for each decal)

How do I do it?

3 Likes

Hello, hope this helps:

local Part1 = script.Parent -- change if the script is not inside Part1

local children = Part1:GetChildren() -- table of children within part

for i, v in pairs(children) do

	if children[i]:IsA("Decal") then -- checks to make sure it's a decal
		children[i].Transparency = 1
	end
end

Use a for loop.

for i, v in pairs(Part1:GetChildren()) do
   v.Transparency = 1
end

Table: {[“index”] = “value”}


i: The index (in this case, the current number of the children being looped through), variable can be renamed
v: The value (in this case, the child), variable can be renamed
for i, v in pairs(Table) do: The loop, this goes through everything. Imagine it like this: for each index, have a value and loop through this table, i want you to do this

Sorry to nitpick, but do not use children[i] if you already have v - it’s like using a = a + b instead of a += b yes, you can use a = a + b but it is a waste of time. Also, wrap the entire code in formatted text using three of these ’ ` ’

1 Like

Thanks for the advice, kind of forgot about using v.

First get a reference to the part, then we loop through all of the children of the part using ipairs(), and check if each child is a Decal object using the IsA() method. If the parts child is a Decal, we then set transparency to 0.5.

Code

local part = game.Workspace.Part1

for _, partChildren in ipairs(part:GetChildren()) do
	if partChildren:IsA("Decal") then
		partChildren.Transparency = 0.5
	end
end