✓ Accepted answer
The other answers haven't addressed putting this into one (property changing) loop, and though in this situation it's perhaps not the best solution, it can be done.
Joining Two Lists
We have two lists:
local walls = game.Workspace.InvisibleWalls:GetChildren()
local doors = game.Workspace.Doors:GetChildren()
We can join them together to make a single list (though this requires a loop since Lua does not have this as a built in function)
local parts = walls
for _, door in pairs(doors) do
table.insert(parts, door)
end
We can now have a single loop that will change the properties of objects in both lists (because we've turned it into just one)
We want all parts to have their CanCollide property set to false, and just those children of Doors to become invisible. It could look like this:
for _, part in pairs(parts) do
if part:IsA("BasePart") then
part.CanCollide = false
if part.Parent == workspace.Doors then
part.Transparency = 1
end
end
end
Or, Using a New Iterator
We can define our own ipairs like iterator for a for loop which will take two lists in and just iterate over both of them. This means we no longer need the initial joining loop, though we do need a more complicated construct..
function joinobjs(one, two)
local i = 0
return function()
i = i + 1
if i <= #one then
return one[i]
elseif i <= #one + #two then
return two[i - #one]
end
end
end
Now we just use the loop from the previous one with a different line for the for:
for part in joinobjs(parts) do
if part:IsA("BasePart") then
part.CanCollide = false
if part.Parent == workspace.Doors then
part.Transparency = 1
end
end
end
2 comments
do I have to use a for loop. I am sure they dont have to be so long and complicated
You have to use some kind of loop to iterate over the contents of a Table, as you want to do. I'll write an answer that contains more typically used code for this.