How would I make a random map choser?

Hey I’m making a obby/death run styled game. So I need a random map choser. I’m not asking for you to do all of it. I’m just asking for an idea. And how I should do it without it lagging out. And an idea were the safest place to store all of these maps in.

Feel free to ask any questions.

The reason I am asking is because I am not a big Scripter or anything but can understand it.

Thanks!

4 Likes

Hey! This should be pretty easy!

All you have to do it put your maps in a table.

Such as:

local maps = {map1, map2, map3, map4}

Then you can use math.random to select a random one.

local mapSelected = maps[math.random(1, #maps)]

6 Likes

And then just a teleport after?

1 Like

Well, then you could move the map to workspace.

mapSelected:Clone().Parent = workspace

I would put all the maps into ServerStorage and group each map.

2 Likes

To do this, you need to make a working round system. Then once you select the map, it should be something like this: (maps in ServerStorage)

local maps = game.ServerStorage.Maps:GetChildren()
local rand = math.random(1,#Maps)
local selectedMap

for i,v in pairs (maps) do
if i==rand then
selectedMap = v:Clone()
break
end
end

selectedMap.Parent = game.Workspace

Hope this helped :slight_smile: The advantage with my system is that you don’t have to update the script every time you add a map, it’s relative.

Alright I would think this is some hard thing but you make it seem pretty simple

Yep, it’s not too hard, you just need a basic understanding how tables and Clone works and you should be fine.

1 Like

To teleport, you simply do(add a part called Teleport, where the players go) :

for i,v in pairs (game.Players:GetChildren()) do
v.Character.HumanoidRootPart.CFrame = game.Workspace.Map.Teleport.CFrame
end
2 Likes

Just to make sure it works at the end could I do print(“Map Loaded”) ?

local maps = {1,2,3,4}
local chosen = maps[math.random(1,#maps)]
chosen.Name = "ChosenMap"
chosen.Parent = workspace
for _, player in pairs(game.Players:GetPlayers()) do
   player.Character.CFrame = workspace.ChosenMap.SpawnPart.CFrame
end
2 Likes

Here’s something I made for you.

You should have a folder inside ServerStorage called “Maps”.

local Maps = game.ServerStorage.Maps:GetChildren() -- Gets the children of maps
local Rand = math.random(1, #maps) -- Picks a random number from 1 to the number of maps

Maps[Rand]:Clone().Parent = workspace -- Picks the random map by indexing it with the random number and cloning it into the workspace.

If you want to teleport the players, use this:

local Maps = game.ServerStorage.Maps:GetChildren() -- Gets the children of maps
local Rand = math.random(1, #maps) -- Picks a random number from 1 to the number of maps

local TeleportPosition = Vector3.new(0, 0, 0) -- This is the position you want to teleport the players.
local TeleportCFrame = CFrame.new(TeleportPosition) -- This is the CFrame you want to teleport players. You don't need to edit this, you only need to edit the TeleportPosition

Maps[Rand]:Clone().Parent = workspace -- Picks the random map by indexing it with the random number and cloning it into the workspace.

for _, Player in pairs(game.Players:GetPlayers()) do -- Loops through all the players in-game.
   if Player.Character then -- Checks if the player has a character. Players might not have a character when they first join in to the game, or when their character is respawning.
      Player.Character:SetPrimaryPartCFrame(TeleportCFrame) -- Sets the character's CFrame to the teleport CFrame. Basically moving the character.
   end
end

I hope this helps.

2 Likes

Thanks to everybody who helped. This really makes it look a lot easier then I thought it would be.

5 Likes

I know to most people this sounds dumb but should I put the script in serverscript service? And I’m guessing it’s a normal script?

ServerScriptService is where you would regularly store your regular scripts. Although it also works in Workspace, putting scripts in ServerScriptService just makes your game organised and easy to access. I’m pretty sure it’s safer to put scripts in ServerScriptService too, as it is not replicated to the clients.

Alright thanks :slight_smile: (30 chars.)

No worries, if you’ve got no more questions relating to the topic of this discussion. Please mark the most appropriate answer as the solution, so other people who stumble upon this post can quickly find the solution. Thanks!

2 Likes

Yes, you should put the script in ServerScriptService. As @slothfulGuy said,

2 Likes

Right an just to make sure its a regular script?

Local scripts do not work within ServerScriptService.

Yeah I realize but just making sure it isn’t something special or like fancy.