Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help with loop crashing game?

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

✓ Accepted answer

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

3 comments

0

Figured that answer from the title #classicmistakes

0

okay, so now it doesn't crash, but now it just stays on the loop. is something wrong?

0

Add a break so it ends the loop.

This discussion is preserved as it was. New answers, comments, and votes are closed.