Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with loop crashing game?

Asked by 8 years ago

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

1 answer

Log in to vote
4
Answered by 8 years ago

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

0
Figured that answer from the title #classicmistakes raspyjessie 117 — 8y
0
okay, so now it doesn't crash, but now it just stays on the loop. is something wrong? Marrleey 55 — 8y
0
Add a break so it ends the loop. SimplyRekt 413 — 8y
Ad

Answer this question