Can't select the same table twice

local StartVotingEvent = VotingEvents.StartVoting
local Maps = {"Camp", "Factory", "Theater"}

function RandomPad1()
--
local RandomMap = Maps[math.random(#Maps)]
MapText1.Text = RandomMap
--
end

function RandomPad2()
--
local RandomMap = Maps[math.random(#Maps)]
MapText2.Text = RandomMap
--
end

function RandomPad3()
--
local RandomMap = Maps[math.random(#Maps)]
MapText3.Text = RandomMap
--
end

StartVotingEvent.Event:Connect(RandomPad1(), RandomPad2(), RandomPad3())

As you can see, I am trying to make a voting system, but when StartVotingEvent is Fired, all three MapText's get randomized and get different names.

But sometimes the names can be the same, for example “MapText1 = Factory, MapText2 = Camp, MapText3 = Factory”

I just want the names to be distributed evenly, no dupes.

Any help is appreciated!
(it’s kind of hard to explain, hopefully i did okay)

1 Like

Can’t you add a table.remove(Maps,RandomMap) under every Map.Text so that there is no possible way for the maps to repeat?

local Maps = {"Name1", "Name2", "Name3", ...}

local function GetRandomMap(AvailableMaps: {string})
   local Index = math.random(#AvailableMaps)
   return Index, AvailableMaps[Index]
end

local function GetThreeRandomMaps()
   local AvailableMaps = table.clone(Maps)
   local SelectedMaps = {}
   
   for _ = 1, 3 do
      local Index: number, Value: string = GetRandomMap(AvailableMaps)
      table.remove(AvailableMaps, Index)
      table.insert(SelectedMaps, Value)
   end

   return SelectedMaps
end

Something like this should work.

Sorry for the late response, but this doesn’t work because the table.remove(Maps, NUMBER) expects a number, not a string. How do you think I should work around this?

This works perfectly, but for some tweaks, how would I check if a string is already in a table?

You can do table.find(TableToLookIn, ValueToFind)

the ValueToFind should be a number, or a string?

Whatever the value is, in this case if it’s the map name you would do string. Though the code I gave you shouldn’t get duplicate maps anyway since it gets removed from the available maps table.

table.find shouldn’t be used as it is very inefficient.

Dictionaries are better since you don’t have to use table.find

What makes it inefficient? Since the table is not a dictionary table.find is the way to go