PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Random Map Chooser

Random Map Chooser

Jul 03 2018, 11:27 AM PST 2 min

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 to do! This article will go over some basic techniques you can use to cycle maps in your game, and how to make the experience smooth for players in your game.

Explorer Setup

A typical setup may look something like this:

MapChooser_Explorer.png

With this setup, each map available in the game is stored in a Folder in the ReplicatedStorage 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")

local 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

local 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

Storing Maps: ReplicatedStorage vs. ServerStorage

In the example above, the Maps were stored in the ReplicatedStorage. Whether you choose to store it in the ReplicatedStorage or ServerStorage is up to you. There are a few things to keep in mind when deciding which technique to use:

  • If your map is static and does not change, then you can simply move it into the Workspace when you want the map loaded, and then move it back into the Maps folder when you’re done.

    • This technique is beneficial because the server will only send the map’s contents to the player one time: while they are joining the game!
  • If your map has moving components and reusing it might not work very well, then you may want to keep it stored in the ServerStorage.

    • Instead of moving it into the Workspace, you would instead Instance/Clone the model into the Workspace.
    • This will make join times faster, but will cause players to experience some lag while the map is being replicated over the network.
Tags:
  • rounds
  • random
  • coding