Random Map System that ignores previous map

Hello, I’m trying to make a random map selector, but I want the system to ignore the previous map. How can I do this?
Script:

local OldMap = workspace:WaitForChild("GrassMap")
local Maps = game:GetService("ReplicatedStorage").Maps:GetChildren()
local ChoosenMap = Maps[math.random(#Maps)]
if ChoosenMap.Name ~= OldMap.Name then
	print(OldMap.Name)
	print(Maps.Name)
	local MapClone = ChoosenMap:Clone()
	MapClone.Parent = workspace
	OldMap:Destroy()
	print(MapClone.Name)
end

Thank you for your attention.

Seems you would either need a list of maps that doesn’t include the previous map, or you’d need to choose again from the same list if the choice matches the prev map.

The first way is easy enough to implement:

local mapList = {}
for _, map in ipairs(Maps) do
    if map.Name ~= OldMap.Name then
        table.insert(mapList, map)
    end
end
-- then choose from one in mapList

Doing it the other way isn’t much more complicated, but it opens the possibility of getting stuck in the loop if your random selection keeps selecting the same old map over and over.

3 Likes