Hi, i'm making a Juggernaut Game with my friend, and the game works, once. All of the players team get changed to sheriff and juggernaut, and the game goes on normally, but then the second round starts, when only the juggernaut gets changed, but the sheriffs don't. Is there any to fix it?

local maps = game.ReplicatedStorage:WaitForChild("Maps")
local juggernaut = nil
local players = game.Players:GetPlayers()
local revolver = game.ReplicatedStorage.Revolver
local knife = game.ReplicatedStorage.Knife
local allplayers = {}
local gamestarted = false
local juggernautplrs = game.ReplicatedStorage.Values.JuggernautPlayers
local sheriffplrs = game.ReplicatedStorage.Values.SheriffPlayers
local spawnspawns = game.Workspace.Spawn.Spawns:GetChildren()
local gameending = false
game.Players.PlayerAdded:Connect(function(plr)
	game.ReplicatedStorage.Values.plrs.Value = game.ReplicatedStorage.Values.plrs.Value+1
	table.insert(allplayers,plr)
end)
game.Players.PlayerRemoving:Connect(function(plr)
	game.ReplicatedStorage.Values.plrs.Value = game.ReplicatedStorage.Values.plrs.Value-1
		table.remove(allplayers,FindIndex(allplayers,plr))
end)
function FindIndex(Table,Val)
	for index,v in pairs(Table) do
		if v == Val then
			return index
		end
	end
end
function loadmap()
	local maplist = maps:GetChildren()
	local map = maplist[math.random(1,#maplist)]
	local nmap = map:Clone()
	local name = nmap.Name
	nmap.Parent = workspace
	nmap.Name = "Map"
	game.ReplicatedStorage.Values.Status.Value = "Chosen Map: "..name
end
function unloadmap()
	if workspace:FindFirstChild("Map") then
		workspace.Map:Destroy()
	end
end
while wait() do
 if game.ReplicatedStorage.Values.plrs.Value >= 2 then
   if gamestarted == false then
  if gameending == false then
	game.ReplicatedStorage.Values.Status.Value = "Loading Map..."
	wait(10)
	loadmap()
    wait(5)
	local juggernaut = allplayers[math.random(1,#allplayers)]
	table.remove(allplayers,FindIndex(allplayers,juggernaut))
	local spawns = game.Workspace:FindFirstChild("Map").Spawns:GetChildren()
	for i,Player in pairs(allplayers) do
		local ngun = revolver:Clone()
		ngun.Parent = Player.Backpack
		Player.TeamColor = BrickColor.new("Electric blue") -- Broken Part
		local Character = Player.Character
		local chosenSpawn = spawns[math.random(1,#spawns)]
		Character:SetPrimaryPartCFrame(chosenSpawn.CFrame + Vector3.new(0,3,0))
	end
	juggernaut.Character:SetPrimaryPartCFrame(spawns[math.random(1,#spawns)].CFrame + Vector3.new(0,3,0))
	local nknife = knife:Clone()
	nknife.Parent = juggernaut.Backpack
	juggernaut.TeamColor = BrickColor.new("Really red")
	juggernautplrs.Value = 1
	sheriffplrs.Value = game.ReplicatedStorage.Values.plrs.Value-1
	gamestarted = true
	game.ReplicatedStorage.Values.Status.Value = juggernaut.Name.." is the juggernaut!"
end
end
else
	game.ReplicatedStorage.Values.Status.Value = "Not enough players"
end
if gamestarted == true then
 if gameending == false then
    if juggernautplrs.Value == 0 then
		game.ReplicatedStorage.Values.Status.Value = "Game has finished, the sheriffs have won!"
gameending = true
     for i,v in pairs(game.Players:GetChildren()) do
	  local hum = v.Character:WaitForChild("Humanoid")
	hum.Health = 0
end
unloadmap()
wait(5)
gamestarted = false
gameending = false
end
if sheriffplrs.Value == 0 then
	game.ReplicatedStorage.Values.Status.Value = "Game has finished, the juggernaut has won!"
gameending = true
    for i,v in pairs(game.Players:GetChildren()) do
	  local hum = v.Character:WaitForChild("Humanoid")
	hum.Health = 0
end
unloadmap()
wait(5)
gamestarted = false
gameending = false
end
end
end
end

2 comments

0

Tab your code correctly, this is awful to read

0

And as for the error, you remove juggernaut from allplayers  and never re-add them again. I would personally get rid of the allplayers table & players events, and instead at the start of every round do players:GetPlayers() instead