Okay, so i made this function to choose 3 random players to compete in a competition. 6 players are supposed to participate in the competition: the competition Host 2 pre-chosen Competitors, and 3 randomly chosen players. I tried to make a while loop so that if the any of the randomly chosen players are the competition host, prechosen competitors, or any of the other randomly chosen players, it would keep on doing math.random until it finds a player that isnt any of those. The thing is, the loop keeps on crashing my game, how would i fix/optimize this code so that it wont crash my game?
function choosePlayersForCompetition() local random1 = math.random(1, #players) local random2 = math.random(1, #players) local random3 = math.random(1, #players) while random1 == table.concat(CompetitionHost) or table.concat(prechosenCompetitor) do table.concat(prechosenCompetitor2) or random2 or random3 then povPlayer = math.random(1, #players) end while povPlayer2 == table.concat(CompetitionHost) or table.concat(prechosenCompetitor) do table.concat(prechosenCompetitor2) or random1 or random3 then povPlayer = math.random(1, #players) end while povPlayer3 == table.concat(CompetitionHost) or table.concat(prechosenCompetitor) do table.concat(prechosenCompetitor2) or random1 or random2 then povPlayer = math.random(1, #players) end local chosenpovPlayer = players[random1] local chosenpovPlayer2 = players[random2] local chosenpovPlayer3 = players[random3] end
Make sure you add a wait
function when doing while loops, otherwise it will crash.
This > SHOULD< work, not entirely sure, didn't look through it completely.
function choosePlayersForCompetition() local random1 = math.random(1, #players) local random2 = math.random(1, #players) local random3 = math.random(1, #players) while random1 == table.concat(CompetitionHost) or table.concat(prechosenCompetitor) do table.concat(prechosenCompetitor2) or random2 or random3 then povPlayer = math.random(1, #players) wait() end while povPlayer2 == table.concat(CompetitionHost) or table.concat(prechosenCompetitor) do table.concat(prechosenCompetitor2) or random1 or random3 then povPlayer = math.random(1, #players) wait() end while povPlayer3 == table.concat(CompetitionHost) or table.concat(prechosenCompetitor) do table.concat(prechosenCompetitor2) or random1 or random2 then povPlayer = math.random(1, #players) wait() end local chosenpovPlayer = players[random1] local chosenpovPlayer2 = players[random2] local chosenpovPlayer3 = players[random3] end