So i have a folder that contains 3 maps with different names, theres this door script that locates the door folder. and each map has a door folder. But idk how i would write this down instead of writing down the name of the map, i want to locate it for all 3 options instead of one. Is there another way?
for _,Door in pairs(workspace.RoundMap.Map1.Doors:GetChildren()) do for _,Door in pairs(workspace.RoundMap.Map2.Doors:GetChildren()) do for _,Door in pairs(workspace.RoundMap.Map3.Doors:GetChildren()) do
You can replace dot syntax with a string index and you would get the same result, and it’s the same for FindFirstChild, but it will return nil instead of erroring, and the same for WaitForChild, except the thread will be suspended waiting for the part to show up:
workspace.RoundMap.Map1 == workspace.RoundMap["Map1"]
-- similar to,
~ workspace.RoundMap:FindFirstChild("Map1")
~ workspace.RoundMap:WaitForChild("Map1")
So you could use a for loop in a table of strings to loop through all your targets:
for _,map in pairs{"Map1", "Map2", "Map3"} do
for _,Door in pairs(workspace.RoundMap[map].Doors:GetChildren()) do
-- code here
end
end
You can also use a numerical for loop to achieve the same thing:
for i = 1, 3 do
for _,Door in pairs(workspace.RoundMap["Map"..i].Doors:GetChildren()) do
-- code here
end
end
If you run into a more abstract use case for what you want to do with the doors, you can turn it into a function:
local function InitializeDoor(door)
-- code here
end
for _,Door in pairs(workspace.RoundMap.Map1.Doors:GetChildren()) do
InitializeDoor(Door)
end