Round ends when bystander killed and map gets cloned by how many players are there

so im trying to make it that the round ends when the shooter dies,that works but for some reason when a player in bystander team dies the round ends, and also map cloned number is the amount of players that are in the server, i only want 1 map to be cloned, heres the script and its in workspace

local rep = game.ReplicatedStorage
local part = workspace:WaitForChild(“map1spawns”):GetChildren()
local part2 = workspace:WaitForChild(“Part3spawns”):GetChildren()
local part3 = workspace:WaitForChild(“spawnszz3”):GetChildren()
local part4 = workspace:WaitForChild(“base4”):GetChildren()
local sound = workspace:WaitForChild(“shooterishere”)

local map1 = rep:WaitForChild(“Baseplate1”)
local map2 = rep:WaitForChild(“Baseplate2”)
local map3 = rep:WaitForChild(“Baseplate3”)
local map4 = rep:WaitForChild(“Baseplate4”)

local lobby = workspace:WaitForChild(“Partlobb”)

local Teams = game:GetService(“Teams”)

local teamA = Teams:FindFirstChild(“Shooter”)
local teamB = Teams:FindFirstChild(“ByStanders”)

local Players = game:GetService(“Players”)
local mapgui = game.StarterGui.map.Frame
local mapguitext = game.StarterGui.map.Frame.TextLabel
local TargetTeam = game:GetService(“Teams”):WaitForChild(“Deads”)

local function teleportPlayerToLocation(player, location)
local character = player.Character or player.CharacterAdded:Wait()
if not character.PrimaryPart then
character:WaitForChild(“HumanoidRootPart”)
character.PrimaryPart = character:FindFirstChild(“HumanoidRootPart”)
end
character:SetPrimaryPartCFrame(location.CFrame)
end

local chosenPlayer = nil
local roundEnded = false

local function onChosenPlayerDied()
roundEnded = true
end

local function assignTeams()
local allPlayers = Players:GetPlayers()
if #allPlayers == 0 then return end

local randomIndex = math.random(1, #allPlayers)
chosenPlayer = allPlayers[randomIndex]

for _, player in ipairs(allPlayers) do
	if player == chosenPlayer then
		player.Team = teamA
	else
		player.Team = teamB
	end
end

-- Hook into chosenPlayer's death
local function connectDeath()
	if chosenPlayer.Character then
		local humanoid = chosenPlayer.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.Died:Connect(onChosenPlayerDied)
		end
	end
end

if chosenPlayer.Character then
	connectDeath()
else
	chosenPlayer.CharacterAdded:Wait()
	connectDeath()
end

end

Players.PlayerAdded:Connect(function(player)
while true do
repeat
local players = game.Players:GetPlayers()
local playersCount = #players
player:WaitForChild(“PlayerGui”):WaitForChild(“player”):WaitForChild(“Frame”).Visible = true
player.PlayerGui.player.Frame.TextLabel.Text = “waiting for 2 players…”
wait(1)
until #game.Players:GetPlayers() >= 0
player.PlayerGui.player.Frame.Visible = false
wait(3)
– 1
local map1clone = map1:Clone()
map1clone.Parent = workspace
player.PlayerGui.map.Frame.Visible = true
wait(3)
teleportPlayerToLocation(player, part[math.random(1, #part)])
roundEnded = false
assignTeams()
wait(1)
player.PlayerGui.map.Frame.Visible = false
local roundTime = 100
for i = 1, roundTime do
if roundEnded then break end
wait(1)
player.PlayerGui.timer.Frame.TextLabel.Text = i
player.PlayerGui.timer.Frame.Visible = true
end

	-- End round
	for _, p in ipairs(Players:GetPlayers()) do
		p.Team = TargetTeam
		teleportPlayerToLocation(p, lobby)
	end
	map1clone:Destroy()	
	sound:Play()
	player.PlayerGui.timer.Frame.Visible = false
	wait(4)

	-- 2
	player.PlayerGui.map2.Frame.Visible = true
	local map2clone = map2:Clone()
	map2clone.Parent = workspace
	wait(3)
	teleportPlayerToLocation(player, part2[math.random(1, #part2)])
	roundEnded = false
	assignTeams()
	wait(1)
	player.PlayerGui.map2.Frame.Visible = false
	for i = 1, roundTime do
		if roundEnded then break end
		wait(1)
		player.PlayerGui.timer.Frame.TextLabel.Text = i
		player.PlayerGui.timer.Frame.Visible = true
	end

	for _, p in ipairs(Players:GetPlayers()) do
		p.Team = TargetTeam
		teleportPlayerToLocation(p, lobby)
	end
	map2clone:Destroy()
	sound:Play()
	player.PlayerGui.timer.Frame.Visible = false
	wait(4)

	-- 3 
	player.PlayerGui.map3.Frame.Visible = true
	local map3clone = map3:Clone()
	map3clone.Parent = workspace
	wait(3)
	teleportPlayerToLocation(player, part3[math.random(1, #part3)])
	roundEnded = false
	assignTeams()
	wait(1)
	player.PlayerGui.map3.Frame.Visible = false

	for i = 1, roundTime do
		if roundEnded then break end
		wait(1)
		player.PlayerGui.timer.Frame.TextLabel.Text = i
		player.PlayerGui.timer.Frame.Visible = true
	end

	for _, p in ipairs(Players:GetPlayers()) do
		p.Team = TargetTeam
		teleportPlayerToLocation(p, lobby)
	end
	map3clone:Destroy()
	player.PlayerGui.timer.Frame.Visible = false
	wait(4)
	-- 4 ---
	player.PlayerGui.map4.Frame.Visible = true
	local map4clone = map4:Clone()
	map4clone.Parent = workspace
	wait(3)
	teleportPlayerToLocation(player, part4[math.random(1, #part4)])
	roundEnded = false
	assignTeams()
	wait(2)
	player.PlayerGui.map4.Frame.Visible = false
	for i = 1, roundTime do
		if roundEnded then break end
		wait(1)
		player.PlayerGui.timer.Frame.TextLabel.Text = i
		player.PlayerGui.timer.Frame.Visible = true
	end

	for _, p in ipairs(Players:GetPlayers()) do
		p.Team = TargetTeam
		teleportPlayerToLocation(p, lobby)
	end
	map4clone:Destroy()	
	sound:Play()
	player.PlayerGui.timer.Frame.Visible = false
	wait(4)
end

end)

Formatted reply

local rep = game.ReplicatedStorage
local part = workspace:WaitForChild(“map1spawns”):GetChildren()
local part2 = workspace:WaitForChild(“Part3spawns”):GetChildren()
local part3 = workspace:WaitForChild(“spawnszz3”):GetChildren()
local part4 = workspace:WaitForChild(“base4”):GetChildren()
local sound = workspace:WaitForChild(“shooterishere”)

local map1 = rep:WaitForChild(“Baseplate1”)
local map2 = rep:WaitForChild(“Baseplate2”)
local map3 = rep:WaitForChild(“Baseplate3”)
local map4 = rep:WaitForChild(“Baseplate4”)

local lobby = workspace:WaitForChild(“Partlobb”)

local Teams = game:GetService(“Teams”)

local teamA = Teams:FindFirstChild(“Shooter”)
local teamB = Teams:FindFirstChild(“ByStanders”)

local Players = game:GetService(“Players”)
local mapgui = game.StarterGui.map.Frame
local mapguitext = game.StarterGui.map.Frame.TextLabel
local TargetTeam = game:GetService(“Teams”):WaitForChild(“Deads”)

local function teleportPlayerToLocation(player, location)
local character = player.Character or player.CharacterAdded:Wait()
if not character.PrimaryPart then
character:WaitForChild(“HumanoidRootPart”)
character.PrimaryPart = character:FindFirstChild(“HumanoidRootPart”)
end
character:SetPrimaryPartCFrame(location.CFrame)
end

local chosenPlayer = nil
local roundEnded = false

local function onChosenPlayerDied()
roundEnded = true
end

local function assignTeams()
local allPlayers = Players:GetPlayers()
if #allPlayers == 0 then return end

local randomIndex = math.random(1, #allPlayers)
chosenPlayer = allPlayers[randomIndex]

for _, player in ipairs(allPlayers) do
	if player == chosenPlayer then
		player.Team = teamA
	else
		player.Team = teamB
	end
end

-- Hook into chosenPlayer's death
local function connectDeath()
	if chosenPlayer.Character then
		local humanoid = chosenPlayer.Character:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.Died:Connect(onChosenPlayerDied)
		end
	end
end

if chosenPlayer.Character then
	connectDeath()
else
	chosenPlayer.CharacterAdded:Wait()
	connectDeath()
end

end

Players.PlayerAdded:Connect(function(player)
while true do
repeat
local players = game.Players:GetPlayers()
local playersCount = #players
player:WaitForChild(“PlayerGui”):WaitForChild(“player”):WaitForChild(“Frame”).Visible = true
player.PlayerGui.player.Frame.TextLabel.Text = “waiting for 2 players…”
wait(1)
until #game.Players:GetPlayers() >= 0
player.PlayerGui.player.Frame.Visible = false
wait(3)
– 1
local map1clone = map1:Clone()
map1clone.Parent = workspace
player.PlayerGui.map.Frame.Visible = true
wait(3)
teleportPlayerToLocation(player, part[math.random(1, #part)])
roundEnded = false
assignTeams()
wait(1)
player.PlayerGui.map.Frame.Visible = false
local roundTime = 100
for i = 1, roundTime do
if roundEnded then break end
wait(1)
player.PlayerGui.timer.Frame.TextLabel.Text = i
player.PlayerGui.timer.Frame.Visible = true
end

	-- End round
	for _, p in ipairs(Players:GetPlayers()) do
		p.Team = TargetTeam
		teleportPlayerToLocation(p, lobby)
	end
	map1clone:Destroy()	
	sound:Play()
	player.PlayerGui.timer.Frame.Visible = false
	wait(4)

	-- 2
	player.PlayerGui.map2.Frame.Visible = true
	local map2clone = map2:Clone()
	map2clone.Parent = workspace
	wait(3)
	teleportPlayerToLocation(player, part2[math.random(1, #part2)])
	roundEnded = false
	assignTeams()
	wait(1)
	player.PlayerGui.map2.Frame.Visible = false
	for i = 1, roundTime do
		if roundEnded then break end
		wait(1)
		player.PlayerGui.timer.Frame.TextLabel.Text = i
		player.PlayerGui.timer.Frame.Visible = true
	end

	for _, p in ipairs(Players:GetPlayers()) do
		p.Team = TargetTeam
		teleportPlayerToLocation(p, lobby)
	end
	map2clone:Destroy()
	sound:Play()
	player.PlayerGui.timer.Frame.Visible = false
	wait(4)

	-- 3 
	player.PlayerGui.map3.Frame.Visible = true
	local map3clone = map3:Clone()
	map3clone.Parent = workspace
	wait(3)
	teleportPlayerToLocation(player, part3[math.random(1, #part3)])
	roundEnded = false
	assignTeams()
	wait(1)
	player.PlayerGui.map3.Frame.Visible = false

	for i = 1, roundTime do
		if roundEnded then break end
		wait(1)
		player.PlayerGui.timer.Frame.TextLabel.Text = i
		player.PlayerGui.timer.Frame.Visible = true
	end

	for _, p in ipairs(Players:GetPlayers()) do
		p.Team = TargetTeam
		teleportPlayerToLocation(p, lobby)
	end
	map3clone:Destroy()
	player.PlayerGui.timer.Frame.Visible = false
	wait(4)
	-- 4 ---
	player.PlayerGui.map4.Frame.Visible = true
	local map4clone = map4:Clone()
	map4clone.Parent = workspace
	wait(3)
	teleportPlayerToLocation(player, part4[math.random(1, #part4)])
	roundEnded = false
	assignTeams()
	wait(2)
	player.PlayerGui.map4.Frame.Visible = false
	for i = 1, roundTime do
		if roundEnded then break end
		wait(1)
		player.PlayerGui.timer.Frame.TextLabel.Text = i
		player.PlayerGui.timer.Frame.Visible = true
	end

	for _, p in ipairs(Players:GetPlayers()) do
		p.Team = TargetTeam
		teleportPlayerToLocation(p, lobby)
	end
	map4clone:Destroy()	
	sound:Play()
	player.PlayerGui.timer.Frame.Visible = false
	wait(4)
end

end)

i have no idea how to do thifs…erm