How to detect if all players is dead except for one

  1. What do you want to achieve? Keep it simple and clear!
    I want to make my round script resets when all players is dead except for the monster

  2. What is the issue? Include screenshots / videos if possible!
    when all soldiers is dead the round won’t reset

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i tried other things like using tables

Here’s my script

 for i = 1, #Players do
		if Players[i].Playing.Value == true and Players[i].isAFK.Value == false then
			if Players[i].Character ~= nil and Players[i].Character:FindFirstChild("Torso") ~= nil and Players[i].Character.Humanoid.Health > 0 and Players[i] ~= Chosen then
				local Spawn = nil
				Spawn = game.Workspace.Map.Spawns.Spawn
				Players[i].PlayerGui.Role.Enabled = true
				Players[i].CameraMode = Enum.CameraMode.LockFirstPerson
				Players[i].Character.HumanoidRootPart.CFrame = CFrame.new(Spawn.Position)
				
				table.insert(Soldiers, Players[i].Name)
				print("Inserted")
				Players[i].PlayerGui.Role.BGF.Niichan.Text = "Soldier"
				Players[i].PlayerGui.Role.Info.InfoChan.Text = "You're a Soldier Sent out by the Goverment to hunt an escapee Mutant,The Mutant is going on a rampage and you need to stop it before it kills more people!"
				Players[i].PlayerGui.Role.BGF.Niichan.TextColor3 = Color3.fromRGB(71, 255, 20)
				
				
				Spawn:Destroy()
				Players[i].Character.Humanoid.WalkSpeed = 0
			elseif Players[i].Character.Humanoid.Health < 0 then
				
				Players[i].Playing.Value = false
				
			elseif Players[i] == Chosen then
				local Spawn = nil
				Spawn = game.Workspace.Map.Spawns.Spawn
				
				Chosen.Monster.Value = true
				Chosen.PlayerGui.Role.Enabled = true
				Chosen.PlayerGui.Role.BGF.Niichan.Text = "Monster"
				Chosen.PlayerGui.Role.Info.InfoChan.Text = "You're a Mutant Created by the Goverment,You Escaped and now the Goverment is Hunting You. Your Goal Here is to survive and kill all the Soldiers.Good Luck!"
				Chosen.PlayerGui.Role.BGF.Niichan.TextColor3 = Color3.fromRGB(255, 0, 4)
				Players[i].Character.HumanoidRootPart.CFrame = CFrame.new(Spawn.Position)
				Players[i].Character.Humanoid.WalkSpeed = 0
			end
		end
	end
	
	script.Parent.CountDown:Play()
	for i = 5, 0, -1 do
		ChangeText('Starting in  ' ..i)
		wait(1)
	end
	

	local Players = game.Players:GetPlayers()
	for i = 1, #Players do
		if Players[i].Playing.Value == true then
			Players[i].Character.Humanoid.WalkSpeed = 16
		end
	end
	
	ChangeText('BEGIN!')
	for i = 1, #Players do
		if Players[i].Playing.Value == true and Players[i].isAFK.Value == false then
			if Players[i].Character ~= nil and Players[i].Character:FindFirstChild("Torso") ~= nil and Players[i].Character.Humanoid.Health > 0 and Players[i] ~= Chosen then

				local Weapon = game.ServerStorage.Weapons:WaitForChild(Players[i]:WaitForChild("Weapon").Value)
				Weapon:Clone().Parent = Players[i].Backpack
				
				
				if Players[i].Character.Humanoid.Health == 0 then
					Players[i].Playing.Value = false
					print("One Dead")
					table.remove(Soldiers,Players[i].Name)
				end
			end
		end
	end
	
	
	for i = GameTime, 0, -1 do -- Time Counter
		if Chosen.Character.Humanoid.Health ~= 0 then
			ChangeText('GameTime ' ..i)
			local waittime = 1
			
			GameWait(waittime)
		elseif Chosen.Character.Humanoid.Health == 0 then
			ChangeText('MUTANT DEAD!')
			Chosen.Playing.Value = false
			Chosen.Monster.Value = false
			break
		elseif #Soldiers < 1 then
			print("Restarting All players")
			break
		elseif Players[i].Character.Humanoid.Health == 0 then
				Players[i].Playing.Value = false
				print("One Dead")
				table.remove(Soldiers,Players[i].Name)
		end
	end
1 Like

Is it showing any errors? cuz your script is looking fine to me

nope it just doesn’t work as i intended it to do

The if … elseif… -thing in your last for loop will never check the number of soldiers. This is because it’ll stop the checks at the first condition when the beast is alive. If you want to check all those things, you’ll need to make separate if statements for some of them. If you just have one if… elseif… -statement, it will stop the checking when it finds one condition that is true.

1 Like

When you use that for loop, you need a start, end and an increase, you’ve only included a start, which is one, an end, which is #Players or the amount of players in the game, and no increase, you need to add an increase value before it works properly.

If you don’t spesify an increase value for a lua for loop, the loop will use a default increase value, which is one, so that’s not a problem.

1 Like

i did it problem solved! i did it problem solved!