Random theme doesn't work

I am making a game with different themes which are randomly selected. It works fine but sometimes it chooses the same theme it chose before. I want it to not choose a theme which was chosen last round. I have tried doing it but couldn’t succeed.

Can anyone help out how to make it work or is there a better way to do this?

Code:

if (not _G.Themes) then -- I am doing this because this script sometimes get disabled and then enabled when round ends.
   _G.Themes = game.ServerStorage.Themes:GetChildren()
end

local themeNum = 1 -- Theme num is 1 for testing
themeChosen = _G.Themes[themeNum]
		
print(lastTheme.Value)
		
table.insert(_G.Themes,lastThemeIndex.Value,lastTheme.Value)
		
table.remove(_G.Themes,themeNum)
		
lastThemeIndex.Value = themeNum
lastTheme.Value = themeChosen
		
print(lastTheme.Value)

Thanks!

Considering that you are using

math.random(min, max)

to randomly choose a theme, you can create a while loop to make sure you don’t get the a repeated theme

local RandomTheme =  math.random(min, max)

while RandomTheme == lastTheme.Value  do 

RandomTheme =  math.random(min, max)
Wait(.1)
end

Edit: Made an error , the ~= is supposed to be ==

1 Like

Don’t put wait in the condition of a while loop.

while RandomTheme == lastTheme.Value do
    RandomTheme = math.random(min, max)
    wait(0.1)
end

Oh, thanks for letting me know! :grinning: