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)