Hello guys in this tutorial I plan on teaching you how to make a successful random map picking script which will do the following:
1.) choose a random map
2.) load the random map
3.) teleport all of the players to a certain spot on the map and back
4.) destroy the map when finished
5.) repeat the cycle of steps 1-4
Getting Started
The first thing we are going to want to do is of course make our maps, when you are done with them make sure to group all of them and call the model “Maps.” (Make sure to make a lobby in Workspace
) it doesn’t have to be a model, you may make it a folder as well, but make sure to parent the model/folder to ServerStorage
. In those maps make sure to add a part and name it “Spawn,” this will be where we want the character to spawn on the map. Next you will want to add a folder into workspace and name it “CurrentMap.” Finally the last thing we need to make is to add a (Server)Script in ServerScriptService
and add a StringValue
named “ChosenMap” as a child of that script. At the end of this your set up should look like this:
Now that your set up is all done, we can start getting into the main part you guys probably wanted to know, the actual script.
First thing we are going to put into our script is to define all of the necessary things needed to make this script work, so that way it is easier and cleaner. Make sure you define the maps, currentmap folder, chosenmap stringvalue, and our spawn back to the lobby. As for players, we will need to define them in our teleport function, so that way it can grab everyone appropriately. When you are done your script should end up like this:
local maps = game.ServerStorage.Maps:GetChildren()
local currentmap = workspace:WaitForChild("CurrentMap")
local chosenmap = script:WaitForChild("ChosenMap")
local spawner = workspace.Lobby:WaitForChild("Spawn") -- match this to your path of your lobby spawner
Messing With The Maps
Now that we have finished defining everything, lets get into our map chooser, this will choose a random map and save its name to the stringvalue we have called “ChosenMap.” In this we are going to create a function and name it chooseMap(), in this function we will use a for loop to look at all of the maps we have in ServerStorage then adding it to a table and checking to see if it is a model just in case. After we have added all of the maps to the table, we will pick a random one using math.random
and set our stringvalue to the name of that map. This is how I set up my function:
function chooseMap()
local choices = {}
for i = 1, #maps do
if maps[i]:IsA("Model") then
table.insert(choices, maps[i])
end
end
local picked = math.random(1,#maps)
chosenmap.Value = choices[picked].Name
end
Next we need to actually load the map into workspace and then delete it when the round is over. I am pretty sure you know what we are going to do for loading the map, yep we are going to clone it into our “CurrentMap” folder by using the value in our “ChosenMap” stringvalue. As for deleting the map we simply search for our “CurrentMap’s” folder and if it is a model, that being our current map, we will destroy it. Here is how I set this up:
function loadMap()
local map = game.ServerStorage.Maps:FindFirstChild(chosenmap.Value):Clone()
map.Parent = currentmap
end
function deleteMap()
for i,v in pairs(currentmap:GetChildren()) do
if v:IsA("Model") then
v:Destroy()
end
end
end
Teleporting The Players And Finishing Everything Up
Now here comes probably the hardest part of this script, teleporting the players to the map and back. This is, as I have said up top, where we will need to define all of the players in the server. When teleporting the player you can use MoveTo()
for their character, but I feel it is just easier to find the HumanoidRootPart
and set its CFrame
to our spawns CFrame on the map that has been chosen. When we are teleporting the player back, it is the same thing, except always having the HumanoidRootPart’s CFrame change to the spawn’s CFrame in the lobby. Here is how I set this up:
function teleportPlayers()
local players = game.Players:GetPlayers()
for i,v in pairs(players) do
v.Character.HumanoidRootPart.CFrame = currentmap:FindFirstChild(chosenmap.Value).Spawn.CFrame
end
end
function teleportBack()
local players = game.Players:GetPlayers()
for i,v in pairs(players) do
v.Character.HumanoidRootPart.CFrame = spawner.CFrame
end
end
Now that all of our functions are set up, we need to call them at the correct time for the script to actually work. We will make a while loop so the script will repeatedly run and in this set the times we want the functions to be called. Change this to your set up, mine is probably different then what yours will be:
while true do
wait(5)
chooseMap()
loadMap()
wait(1)
teleportPlayers()
wait(20)
teleportBack()
deleteMap()
end
Extras
I hope this long tutorial was helpful! If you had a hard time trying to understand some things or had a hard time creating this script, you can take a look at this downloadable file so you can look over and review it: Random Map Picker.rbxl (22.4 KB)
Thanks For Reading I Hope This Was Helpful!