Map Chooser

If you've ever played a Roblox game with rounds, you may have noticed that they offer a wide variety of maps, and games seem to choose them randomly. You may be wondering how its possible to make something like this, but its actually quite simple.

A typical setup may look something like this:

  • Tree-collapse.png
    ReplicatedStorage
    ReplicatedStorage
    • Tree-collapse.png
      Folder
      Maps
      • Tree-collapse.png
        Model
        Haunted Mansion
        • Tree-collapse.png
          Model
          Spawns
          • SpawnLocation
            SpawnLocation
          • SpawnLocation
            SpawnLocation
          • SpawnLocation
            SpawnLocation
          • SpawnLocation
            SpawnLocation
        • Expand.png
          Model
          Geometry
      • Tree-collapse.png
        Model
        Abandoned Factory
        • Tree-collapse.png
          Model
          Spawns
          • SpawnLocation
            SpawnLocation
          • SpawnLocation
            SpawnLocation
          • SpawnLocation
            SpawnLocation
          • SpawnLocation
            SpawnLocation
        • Expand.png
          Model
          Geometry

With this setup, each map available in the game is stored in a ExplorerImageIndex77.pngFolder in the ExplorerImageIndex70.pngReplicatedStorage called Maps. Here are a few sample functions, which allow you to load a random map into the world (and then later unload it) using this setup.

local replicatedStorage = game:GetService("ReplicatedStorage")
local maps = replicatedStorage:WaitForChild("Maps")
 
function pickMap()
	local mapList = maps:GetChildren() -- Declares a table of the maps inside of the Maps folder.
	local selectedIndex = math.random(1,#maps) -- Choose a random number between 1, and the number of maps available.
	local map = mapList[selectedIndex]:Clone() -- Create a clone of the map we selected.
	map.Parent = workspace -- Parent it to the workspace.
	map.Name = "Map" -- Rename the map so that we can use the unloadMap() function later to remove it.
	return mapList[selectedIndex].Name -- Return the name of the map we selected, in case we want to display it.
end
 
function unloadMap()
	if workspace:FindFirstChild("Map") then -- If there is a model in the Workspace named "Map"
		workspace.Map:Destroy() -- Destroy it!
	end
end
 
local h = Instance.new("Hint")
h.Parent = workspace
 
while true do
	h.Text = "Picking new map..."
	wait(3)
	local mapName = selectMap()
	h.Text = "Selected map: "..mapName
	wait(3)
	h.Text = "Unloading map..."
	unloadMap()
	wait(3)
end