How would i find a model inside a model?

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 could have a IntValue for the current map, and when a game starts, just do

for _,Door in pairs(workspace.RoundMap1:FindFirstChild("Map"..workspace.RoundMap1.Map.Value).Doors:GetChildren() --Code end

1 Like

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
1 Like

So, if the Workspace resembles the image below, just do:

image:
image

--code above--
if Door.Name == *map 1 name* then
     --code inside--
elseif Door.Name == *map 2 name* then
     --code inside--
elseif Door.Name == *map 3 name* then
     --code inside--
end
--code underneath--
1 Like