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
Example:
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)
Like:
(0,1,2,3) â Use for numbers â
(a,b,c,d) â Use for alphabets â
(script.Parent.Parent,script.Parent,script) â Use for Instances â
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.
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 )
â
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?
using math.random() passing just one argument, the number returned is a random number selected through 1 till the number provided.
Also
@Cytheur you donât need to use WaitForChild() for direct children under ServerStorage, contents exist implicitly
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)