I was wondering if anyone can help
01 | local remoteEvent = waitForChild(game:GetService( "ReplicatedStorage" ), "Event" ) |
02 | local mapsStorage = waitForChild(game:GetService( "ServerStorage" ), "Maps" ):GetChildren() -- I want this bit to randomly choose a map instead of only choosing the first one |
03 |
04 | local playersService = game:GetService( "Players" ) |
05 |
06 | function minigameModule.isPotentialGame() |
07 | return playersService.NumPlayers > = settingsModule.minimumPlayers |
08 | end |
09 |
10 | function minigameModule:chooseMap() |
11 | local chosenMap = mapsStorage [ #mapsStorage ] :Clone() |
12 | if findFirstChild(chosenMap, "Spawns" ) then |
13 | chosenMap.Parent = workspace |
14 | chosenMap:MakeJoints() |
15 | self.currentMap = chosenMap |
16 | return chosenMap |
17 | end |
18 | end |
The length unary operator #
returns the length of the operand; in this case, the operand is your table mapStorage
. Since the length of the table is (presumably) fixed, the number it returns will be constant. So if mapStorage
has a length of 5, you're always accessing the fifth element in the table w/ mapsStorage[#mapsStorage]
.
You want to pick one of the maps randomly, we can achieve this using the Random
datatype.
01 | local remoteEvent = waitForChild(game:GetService( "ReplicatedStorage" ), "Event" ) |
02 | local mapsStorage = waitForChild(game:GetService( "ServerStorage" ), "Maps" ):GetChildren() -- I want this bit to randomly choose a map instead of only choosing the first one |
03 |
04 | local playersService = game:GetService( "Players" ) |
05 |
06 | local R = Random.new() |
07 |
08 | function minigameModule.isPotentialGame() |
09 | return playersService.NumPlayers > = settingsModule.minimumPlayers |
10 | end |
11 |
12 | function minigameModule:chooseMap() |
13 | local chosenMap = mapsStorage [ R:NextInteger( 1 , #mapsStorage) ] :Clone() |
14 | if findFirstChild(chosenMap, "Spawns" ) then |
15 | chosenMap.Parent = workspace |
16 | chosenMap:MakeJoints() |
17 | self.currentMap = chosenMap |
18 | return chosenMap |
19 | end |
20 | end |
local chosenMap = mapsStorage[#mapsStorage]:Clone()
What are you doing heere is just referencing the table's last element, which I suppose you do not want.
I assume you want a random element.
If that's the case, then you should use math.random(1,#mapsStorage)
This way, the function will return a value between the table's first and last element.
So, concluding:
local chosenMap = mapsStorage[math.random(1,#mapsStorage)]:Clone()
Please, upvote this if you find this useful.