Random selected a map

does anyone know how I can make the game randomly select a map and teleport you but after surviving teleport to the lobby

Use a folder and use getchildren function to create a table
Then use random number

local Folder = *** map folder *** --- a Folder Class Instance ---
local Maps = Folder:GetChildren()
local Map = Maps[math.random(1,#Maps)] --- The chosen map ---

This is for selecting map.

Table is array, it is like a storage. (Or a box)
(0,1,2,3) — Use for numbers —
(a,b,c,d) — Use for alphabets —
(script.Parent.Parent,script.Parent,script) — Use for Instances —

1 Like

I suggest studying arrays of objects (the map) and the math library (to select a random map from the array) before trying to program that.

1 Like

You’d first want to have a Folder filled with a variety of different maps, preferably stored inside of ServerStorage.

For example, we can name it MapsFolder.

Once maps are added inside of this folder, you’ll want to create a variety of different spawn points inside of this map. Place them inside of a separate folder in each map. Name this folder of Spawns, Spawns

Lastly, make sure you have a lobby within your game, with its own set of spawn points similarly to how your maps are set up with spawns.

While I won’t write the full on code, you’ll want something along these lines. ( forgive mistakes, currently on mobile in bed :slight_smile: )

Pseudo Code
  • Begin an infinite loop, for example a while true do loop.
  • After a short amount of time, draw a random map from the MapsFolder. Here’s an example

local MapsFolder = game:GetService(“ServerStorage”):WaitForChild(“MapsFolder”):GetChildren()
local RandomMap = math.random(#MapsFolder)
local ChosenMap = MapsFolder[RandomMap]

Now that we have a random map under the variable ChosenMap. You’ll want to teleport the players, to do so you’d need to

  • loop through current players within Players service
  • select a random spawn within ChosenMap ( done similarly to drawing a random map, just with the spawns folder instead ).
  • After spawn is chosen, teleport player to one of the random spawns.

Finally, you’ll just want to wait a duration until ultimately teleporting players back to the Lobby!

Good luck!


Why math.random(#MapsFolder)?
I think it should be math.random(1,#MapsFolder),isn’t it?

1 Like

using math.random() passing just one argument, the number returned is a random number selected through 1 till the number provided.


@Cytheur you don’t need to use WaitForChild() for direct children under ServerStorage, contents exist implicitly

1 Like

math.random(#MapsFolder) and math.random(1,#MapsFolder) would ultimately yield the same results as I believe ‘math.random’ always begins with 1 by default if you put nothing there.

Therefore, saying math.random(1,#MapsFolder) is ultimately the same as saying math.random(#MapsFolder)