Script that will give 3 random, and different values

So, I am trying to make a vote gui and I need help with a part of one of the scripts:

local items = game.ReplicatedStorage.Maps:GetChildren()
		local map1 = items[math.random(1, #items)]
		local map2 = items[math.random(1, #items)]
		local map3 = items[math.random(1, #items)]
		if map2.Name == map1.Name then
			repeat 
				local map2 = items[math.random(1, #items)]
			until map2.Name ~= map1.Name
		end
		if map3.Name == map1.Name or map3.Name == map2.Name then
			repeat 
				local map3 = items[math.random(1, #items)]
			until map3.Name ~= map1.Name and map3.Name ~= map2.Name
		end

I thought that this would work, but sometimes some of the maps are the same. I probably did something stupid but I really can’t find what, thanks for all the help in advance!

Instead, procedurally remove the selected map from the list and then loop again and select a new one from the new list. That would save some time from the random loops in analog of trying to randomly press the key in a keyhole.

1 Like

That makes sense, Imma try that. Thanks!

You can use table.remove()

local alreadyChosen = {}
local items = game.ReplicatedStorage.Maps:GetChildren()
		local map1 = items[math.random(1, #items)]
		local map2 = items[math.random(1, #items)]
		local map3 = items[math.random(1, #items)]
table.insert(alreadyChosen, map1)
table.insert(alreadyChosen, map2)
table.insert(alreadyChosen, map3)
table.remove(items, map1)
table.remove(items, map2)
table.remove(items, map3)

Would it work if I did something like this:

		local items = game.ReplicatedStorage.Maps:GetChildren()
		local map1 = items[math.random(1, #items)]
		map1.Parent = nil
		items = game.ReplicatedStorage.Maps:GetChildren()
		local map2 = items[math.random(1, #items)]
		map2.Parent = nil
		items = game.ReplicatedStorage.Maps:GetChildren()
		local map3 = items[math.random(1, #items)]
		map1.Parent = game.ReplicatedStorage.Maps
		map2.Parent = game.ReplicatedStorage.Maps

Hmmm, yes. That would work but not quite exactly. However, what I meant exactly is this:

local items = game.ReplicatedStorage.Maps:GetChildren()
local selectedMaps = {}
for i = 1, 3 do
    local index = math.random(1, #items)
    table.insert(selectedMaps, items[index])
    table.remove(items, index)
end