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

Repetition of :GetChildren() in a function?

So what i'm basically aiming to do is repeat the function :GetChildren(), because in my models there are parts, and in those parts there are decals. The code is below;

wait(1)
kit = script.Parent.Parent.Parent.Plane.Value
SeatbeltOn = kit.SEATBELT.SEATBELTSON:GetChildren()
SeatBeltOnDecal = SeatbeltOn:GetChildren()
SeatbeltOff = kit.SEATBELT.SEATBELTSOFF:GetChildren()
SeatBeltOffDecal = SeatbeltOff:GetChildren()
script.Parent.MouseButton1Click:connect(function(onClick)
	for _,v in pairs (SeatBeltOnDecal) do
		v.Transparency = 0
	end
	for _,v in pairs (SeatBeltOffDecal) do
		v.Transparency = 1
	end
	kit.SEATBELT.Sound:Play()
end)

However ROBLOX's code won't let me do that, and i'm curious if it's possible. I'd be happy if i got help, if you need any further details feel free to comment and i'll reply ASAP.

2 answers

✓ Accepted answer

Okay, so I actually wrote a wrapper for this ages ago but I don't want to do it again because I really hate people and wrappers are cool but I don't write wrappers anymore because Valkyrie.

More to the point, you can get the descendants if that's what you want:

local function GetDescendants(i)
    local queue = {};
    local returns = {};
    local v = i;
    while v do
        local t = v:GetChildren();
        for i = 1, #t do
            local v = t[i]
            returns[#returns+1] = v;
            queue[#queue+1] = v;
        end;
        v = queue[#queue];
        queue[#queue] = nil;
    end;
    return returns;
end;

Use that in place of your original GetChildren, and it will return the children and the children of children. You can then check for if the descendant is a decal.

-- Stick the GetDescendants function somewhere up here.
wait(1)
kit = script.Parent.Parent.Parent.Plane.Value
SeatbeltOn = GetDescendants(kit.SEATBELT.SEATBELTSON) -- Here!
SeatbeltOff = GetDescendants(kit.SEATBELT.SEATBELTSOFF) -- Here!
script.Parent.MouseButton1Click:connect(function(onClick)
    for _,v in pairs (SeatBeltOn) do
		if v:IsA('Decal') then
        	v.Transparency = 0
		end;
    end
    for _,v in pairs (SeatBeltOff) do
		if v:IsA('Decal') then
        	v.Transparency = 1
		end;
    end
    kit.SEATBELT.Sound:Play()
end)

Source

2 comments

0

In addition, I'd like to bring up the alternative of using recursion to iterate through something multiple times. However, I do find using loops a lot better for this when possible.

0

I don't consider recursion an alternative. Recursion should not even be considered where an iterable solution exists.

In fact, calling :GetChildren() over a table isn't a very smart move. All you need is to:

#1. Call :GetChildren() over every child: You may do this using the pairs iterator as you done already

for i,v in pairs (SeatbeltOn) do
	local ["t"..i] = v:GetChildren()
end

#2. Joining all part tables in a single table Create an empty table and insert every table content you generated on the previous step. Use pairs iterator:

for i,v in pairs (SeatbeltOn) do
	local ["t"..i] = v:GetChildren()
	if i == #SeatbeltOn then
		local num = i
	end
	SeatBeltOnDecal = {} -- needs to be global so you can use everywhere on the script
	for f = 1,num do
		for i,v in pairs (["t"..f]) do
			table.insert(SeatBeltOnDecal,i,v)
		end
	end
end

NOTE: The data order may get messed up, but since it does not matter, there is no problem

#3. Use your table to the purpose you wanted. Here it is your table ready! Don't forget to repeat the process to the other table. Hope I helped!

1 comment

0

Your code is wrong and bad, and you should feel bad.

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