How do I add 2 random killers?

Hey! I made a game script that chooses a random killer. I want to know how to make it compatible with 2 killers.

I am afraid that there is a possibility that you can be chosen killer twice.


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")

local Cooldown = wait(3)
local Status = ReplicatedStorage.Status
local VotingSystem = game.Workspace.Voting
local Choices = VotingSystem:GetChildren()
local Maps = ReplicatedStorage.Maps:GetChildren()
local Intermission = 15

local LobbyTeam = Teams["Lobby"]
local RedTeam = Teams["Killer"]
local BlueTeam = Teams["Runner"]
local RedTeamColor = RedTeam.TeamColor
local BlueTeamColor = BlueTeam.TeamColor
local RedTeamCount
local BlueTeamCount

local IsAnOption
local RandomMap
local ChosenMap
local MapClone
math.randomseed(tick())

local function PickRandomMap()
    local RandomNumber = math.random(1,#Maps)
    RandomMap = Maps[RandomNumber]
    return RandomMap.CanBeVoted
end 

for Index, Choice in pairs(Choices) do
    local Name = Choice.label.SurfaceGui.TextLabel
    local Picture = Choice.Image.SurfaceGui.ImageLabel

    IsAnOption = PickRandomMap()

    if IsAnOption.Value == true then
        repeat 
            IsAnOption = PickRandomMap()
        until
        IsAnOption.Value == false
        Name.Text = RandomMap.Name
        Picture.Image = RandomMap.Image.Value
        RandomMap.CanBeVoted.Value = true
    else
        Name.Text = RandomMap.Name
        Picture.Image = RandomMap.Image.Value
        RandomMap.CanBeVoted.Value = true
    end					
end

ReplicatedStorage.InRound.Changed:Connect(function()
        -- Choosing Map        
        if ReplicatedStorage.InRound.Value == false then
            MapClone:Destroy()

            for Index, Map in pairs(Maps) do
                Map.CanBeVoted.Value = false
            end

            for Index, Choice in pairs(Choices) do
                local Name = Choice.label.SurfaceGui.TextLabel
                local Picture = Choice.Image.SurfaceGui.ImageLabel

                IsAnOption = PickRandomMap()

                if IsAnOption.Value == true then
                    repeat 
                        IsAnOption = PickRandomMap()
                    until
                    IsAnOption.Value == false
                    Name.Text = RandomMap.Name
                    Picture.Image = RandomMap.Image.Value
                    RandomMap.CanBeVoted.Value = true

                else
                    Name.Text = RandomMap.Name
                    Picture.Image = RandomMap.Image.Value
                    RandomMap.CanBeVoted.Value = true		
                end					
            end	
        else
            local Choice1Votes = #VotingSystem.Choice1.button.Votes:GetChildren()
            local Choice2Votes = #VotingSystem.Choice2.button.Votes:GetChildren()
            local Choice3Votes = #VotingSystem.Choice3.button.Votes:GetChildren()

            if Choice1Votes >= Choice2Votes and Choice1Votes >= Choice3Votes then

                ChosenMap = VotingSystem.Choice1.label.SurfaceGui.TextLabel.Text

            elseif Choice2Votes >= Choice1Votes and Choice2Votes >= Choice3Votes then

                ChosenMap = VotingSystem.Choice2.label.SurfaceGui.TextLabel.Text

            else

                ChosenMap = VotingSystem.Choice3.label.SurfaceGui.TextLabel.Text

            end
            Status.Value = "The Chosen map is: ".. ChosenMap”
            for Index, Map in pairs(Maps) do
                if ChosenMap == Map.Name then
                    MapClone = Map:Clone()
                    MapClone.Parent = game.Workspace
                end
            end	
            Cooldown

            for Index, Choice in pairs(Choices) do
                Choice.label.SurfaceGui.TextLabel.Text = " "
                Choice.Image.SurfaceGui.ImageLabel.Image = " "
                Choice.button.Votes:ClearAllChildren()
                ReplicatedStorage.VoteReset:FireAllClients(Choice.button)
            end
            -- Teams
            local BlueTeamCount = {}
            local RedTeamCount = {}
            local PlayerList = {}

            local PreviousKiller : Player? = nil

            local function PickKiller()
                for _, player : Player in game:GetService("Players"):GetPlayers() do
                    if player ~= PreviousKiller then
                        table.insert(PlayerList, player)
                    end
                end
                local RandomIndex = math.random(1, #PlayerList)
                local SelectedKiller = PlayerList[RandomIndex]
                PreviousKiller = SelectedKiller
                table.clear(PlayerList)

                return SelectedKiller
            end

            local Killer = PickKiller()

            for _, Player in pairs(Players:GetChildren()) do
                local char = Player:WaitForChild("Character")
                local root = char:WaitForChild("HumanoidRootPart")
                local RedSpawns = MapClone.RedSpawns:GetChildren()
                local BlueSpawns = MapClone.BlueSpawns:GetChildren()
                local RandomBlueSpawn = BlueSpawns[math.random(1,#BlueSpawns)]   
                local RandomRedSpawn = RedSpawns[math.random(1,#RedSpawns)]
                local gui = char.head:FindFirstChild("NameGUI")
                -- Check if the player is a killer
                if Player == Killer then
                    Player.Team = RedTeam
                    root.CFrame = RandomRedSpawn.CFrame + Vector3.new(math.random(1,3), math.random(1,3), math.random(1,3))
                    Player.CameraMode = "LockFirstPerson"                    
                    Player.CameraMaxZoomDistance = 9
                    table.insert(RedTeamCount, Player.Name)
                    if gui then
                        gui.Name.TextColor3 = RedTeamColor
                    end
                    print(Player.Name.." Inside: "..Player.Team.Name)
                elseif Player ~= Killer then
                    Player.Team = BlueTeam
                    Player.CameraMode = "Classic"
                    Player.CameraMaxZoomDistance = 9
                    root.CFrame = RandomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))
                    table.insert(PlayerList, Player.Name)
                    table.insert(BlueTeamCount, Player.Name)
                    if gui then
                        gui.Name.TextColor3 = BlueTeam
                    end
                end
                for Index, Player in pairs(Players:GetChildren()) do
                    local Character = Player.Character
                    local RootPart = Character:WaitForChild("HumanoidRootPart")
                    local NameGUI = Character.Head:FindFirstChild("NameGUI")
                    -- Spawns
                    local RedSpawns = MapClone.RedSpawns:GetChildren()
                    local BlueSpawns = MapClone.BlueSpawns:GetChildren()

                    local RandomRedSpawn = RedSpawns[math.random(1,#RedSpawns)]
                    local RandomBlueSpawn = BlueSpawns[math.random(1,#BlueSpawns)]  
                    Character:WaitForChild("Humanoid").Died:Connect(function()
                            Player.Team = LobbyTeam
                            Player.CameraMode = "Classic"
                            Player.CameraMaxZoomDistance = 9
                        end)
                end
            end
        end)

local function PickKiller(amount: number)
   local ListOfPlayers = Players:GetPlayers()
   local Killers = {}

   -- If there was a previous killer and previous killer is in the player list then remove them
   if PreviousKiller and table.find(ListOfPlayers, PreviousKiller) then
      table.remove(ListOfPlayers, table.find(ListOfPlayers, PreviousKiller))
   end

   -- Iterates through the amount sent to get the killers
   for i = 1, amount do
      local RandomIndex = math.random(#PlayerList)
      local SelectedKiller = PlayerList[RandomIndex]
      table.insert(Killers, SelectedKiller)
   end

   -- If Killers is more than one then PreviousKiller is set to a table
   PreviousKiller = #Killers > 1 and Killers or Killers[1]
   
   table.clear(PlayerList)

   return Killers
end

-- 1 Killer
PickKiller(1)

-- 2 Killers
PickKiller(2)
1 Like

How do I make it only enable 2 killers when there are 6 or more players in the game?

local Killer = #Players:GetPlayers() > 6 and PickKiller(2) or PickKiller(1)

Basically, if the length of the Players table is greater than 6 it will pick 2 killers else if will pick one.

1 Like

I had a issue where it said invalid argument for random (interval empty)

Sorry for the late reply, hopefully you fixed it but if not. The line local RandomIndex = math.random(#PlayerList) should be local RandomIndex = math.random(#ListOfPlayers) and the line after you change the PlayerList to ListOfPlayers

1 Like

Another error. It doesnt put you in a team now.