I made a script that chooses a random killer and the random killer cant be choosen the second round. That means if you were previously the killer you can’t be killer again until someone else becomes killer. The problem is there is no previous killer the first round, and instead the system just makes both of the players killers, then changes them to runners second round and repeats.
How would I fix this?
Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
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
wait(3)
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
end)