Picking three Maps from Map list

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. :smiley:

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

Thanks for any help!

Couldn’t you just do:

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

Hope this helps :smiley:
(Note: I havent tested this)

1 Like

Thank you, that works.

But then I need to define this:

    local RandomMap1 = SelectedMaps[1]
    local RandomMap2 = SelectedMaps[2]
    local RandomMap3 = SelectedMaps[3]

Which cannot be done with your method, because I cannot know what name the selected map is.

image
Source: How do I get first table value in Lua - Stack Overflow

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

Thanks a lot for help!

I did it like this and it works!

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
1 Like