Hello, so I am trying to pick three maps from a Folder of Maps.
They’re 3 randomly selected maps, which will people vote on.
The thing is, I don’t want the maps to be duplicated, I tried many solutions, yet nothing worked.
This is the last solution I tried, I ran into Script Exhausted – that doesn’t mean any good.
SelectedMaps is a Table, by the way.
function VotingModule:SelectMaps()
local Maps = ServerStorage.Maps:GetChildren()
for i = 1,3 do
local RandomMap = Maps[math.random(1,#Maps)]
if i == 1 then
SelectedMaps[1] = RandomMap.Name
elseif i == 2 then
if RandomMap.Name ~= SelectedMaps[1] then
SelectedMaps[2] = RandomMap.Name
else
VotingModule:SelectMaps()
end
elseif i == 3 then
if RandomMap.Name ~= SelectedMaps[1] and RandomMap.Name ~= SelectedMaps[2] then
SelectedMaps[3] = RandomMap.Name
else
VotingModule:SelectMaps()
end
end
end
end
function VotingModule:SelectMaps()
local Maps = ServerStorage.Maps:GetChildren()
local Chosen = {}
for i = 1, 3 do
local Map = Maps[math.random(#Maps)]
while Chosen[Map.Name] do
Map = Maps[math.random(#Maps)]
end
Chosen[Map.Name] = Map
end
-- Chosen is a table with 3 maps stored under their names
end
After you have the chosen table, you would need to iterate through the table and set the maps, like so:
function VotingModule:SelectMaps()
local Maps = ServerStorage.Maps:GetChildren()
local Chosen = {}
local RandomMaps = {}
for i = 1, 3 do
local Map = Maps[math.random(#Maps)]
while Chosen[Map.Name] do
Map = Maps[math.random(#Maps)]
end
Chosen[Map.Name] = Map
end
local index = 1
for name, map in pairs(Chosen) do
RandomMaps[index] = name -- change from name to map if you want the map object, not just the map name
end
local RandomMap1, RandomMap2, RandomMap3 = unpack(RandomMaps)
end
function VotingModule:SelectMaps()
local Maps = ServerStorage.Maps:GetChildren()
local Chosen = {}
for i = 1, 3 do
local Map = Maps[math.random(#Maps)]
while Chosen[Map.Name] do
Map = Maps[math.random(#Maps)]
end
Chosen[Map.Name] = Map.Name
SelectedMaps[i] = Chosen[Map.Name]
end
end