I have a script that randomly chooses 2 players. In this script, if the randomly chosen players are the same, it will re-pick the second player until it is a different player. If there is only 1 player present in the game, however, the script will time out from a loop. I need help fixing this so that the script will not break or timeout.
(Lines 7-10 is the loop that will break the script if 1 player is present in the server.)
Here's the script:
local function choosePlayers()
local users = game.Players:GetPlayers()
local user1 = users[math.random(1, #users)]
local user2 = users[math.random(1, #users)]
if user1 == user2 then -- the same player might get selected twice.
while user1 == user2 do
-- This loop is entered only if the above is true.
user2 = users[math.random(1, #users)]
end
end
return user1, user2 -- return the selected users for convenience
end
while true do
wait(3)
local user1, user2 = choosePlayers()
print(user1, user2)
end

1 comment
at line 9 do if user1 ~= user2 then break end