There is a better way to do this, you can create table with your game modes and select random game mode from it, if the selected game mode is equal to the previously selected game mode you select the game mode next to it.
local modes = {
"Classic Mode",
"Hardcore Mode",
"Speed Mode",
"Crazy Mode",
"Slow Mode",
"Super Mode",
"Penguin Mode"
}
Here is how to get each of the game modes:
print( modes[1] )
print( modes[2] )
print( modes[3] )
print( modes[4] )
print( modes[5] )
print( modes[6] )
print( modes[7] )
You can generate random number from 1 to 7 (or instead of 7 you can use #modes which is the amount of modes available) to get the random game mode:
print( modes[ math.random(1, #modes) ] )
print( modes[ math.random(1, #modes) ] )
print( modes[ math.random(1, #modes) ] )
print( modes[ math.random(1, #modes) ] )
print( modes[ math.random(1, #modes) ] )
print( modes[ math.random(1, #modes) ] )
print( modes[ math.random(1, #modes) ] )
Here is a way to get rid of duplicates:
local lastMode = nil
local function getRandomMode()
local index = math.random(1, #modes)
local mode = modes[index]
if mode == lastMode then
mode = modes[index % #modes + 1]
end
lastMode = mode
return mode
end
If number of previous mode is 7 and it generates number 7 again, 7 + 1 would be 8, such game mode does not exist, that's why it goes back to 1, this is what the index % #modes + 1 does:
print( 1 % #modes + 1 )
print( 2 % #modes + 1 )
print( 3 % #modes + 1 )
print( 4 % #modes + 1 )
print( 5 % #modes + 1 )
print( 6 % #modes + 1 )
print( 7 % #modes + 1 )
The whole code:
local modes = {
"Classic Mode",
"Hardcore Mode",
"Speed Mode",
"Crazy Mode",
"Slow Mode",
"Super Mode",
"Penguin Mode"
}
local lastMode = nil
local function getRandomMode()
local index = math.random(1, #modes)
local mode = modes[index]
if mode == lastMode then
mode = modes[index % #modes + 1]
end
lastMode = mode
return mode
end
print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )
1 comment
thanks for the anser,i cant get it to work, mission aborted.