How do I not get the same player

I want this script to get two different players, but I sometimes still get the same player.

local inGame = {}
local imposters= {}

function chooseTheImposter()
	local getPlayers = player:GetChildren()
	local chosenPlayer
	for i, players in pairs(getPlayers) do
		if inGame[i] == players then
			if #getPlayers >= 2 and #getPlayers < 10 then
				local chosen = math.random(1, #getPlayers)
				chosenPlayer = getPlayers[chosen]
				table.insert(imposters, chosenPlayer)
				local secondChosen = math.random(1, #getPlayers)
				chosenPlayer = getPlayers[chosen]
				table.insert(imposters, chosenPlayer)
				print(imposters)
				return
			elseif #getPlayers >= 10 then
				local chosen = math.random(1, #getPlayers)
				chosenPlayer = getPlayers[chosen]
				table.insert(imposters, chosenPlayer)
				return
			end
		end
	end
end

You could make the choose function choose a singular imposter. Then, just call it twice, like this:

local imposters = {}

local function chooseAnImposter()
    local players = game.Players:GetChildren()
    local chosen = players [math.random(1, #players)]

    if not imposters[chosen] then
        return chosen
    else
        return chooseAnImposter() -- this will re-run the function until a different player is chosen.
    end
end

for i = 1, 2 do
    table.insert(imposters, chooseAnImposter())
end
2 Likes

What if you inserted all of the player’s into a table, then you take a random player from the table, and use table.remove to remove that player from the table. That way when you go to choose the second player the first player you picked is no longer in the available players table. If that doesn’t make sense I can explain it more thoroughly.

1 Like

Thanks you just gave me an idea and its working.

1 Like