How to Select a Random Number that Isn't the Same as the Last

I’m trying to work out how I’d make a map selector from the number of maps inserted into a table, but the problem is that if I select a random number twice, it could end up being the same number. I have an example script if this doesn’t make sense:

local Table = {}
for i, v in pairs(folder:GetChildren())--Random folder with maps
table.insert(v)
end

function ChooseMaps ()
local X = math.random(1,#Table)
local Y = math.random(1,#Table)
if Y == X then
--Go back to line above somehow
end
end
2 Likes
local Y
repeat Y = math.random(1,#Table) 

until Y ~= X
3 Likes

You can store the randomized number in a variable. Then, do a repeat until loop and when it is not the stored number, break the loop then change the stored number to the new number.

Just save the last map And if the next chosen map is that map then it just picks again until it’s a diff map

Instead of using a number for each map in a table, you can simply use the :GetChildren() of said folder to select within the folder itself.

local selected = folder:GetChildren()[math.random(1,#folder:GetChildren())]

If you want to have it exclude the last chosen map, just take it out of the folder and place it somewhere else and put it back after you’ve selected.
Something like this, I guess:
Let’s say “chosen” is a separate folder that holds the previously chosen map.

local selected = folder:GetChildren()[math.random(1,#folder:GetChildren())]
for i,c in pairs(chosen:GetChildren()) do
c.Parent = folder
end
selected.Parent = chosen
--Place any maps excluded from "folder" previously back into folder and place the newly chosen one into "chosen." Any maps in "chosen" will not be selected from by "selected," ect.
1 Like