Ending a minigame once everyone died?

I’m working on a game that swaps out minigames every time everyone loses, or time runs out. Problem is, I want to be able to reset the game entirely once everyone playing the minigame dies. Here is my check script currently:

local zone1Walls = script.Parent.Parent.Zone1Walls
local zone2Walls = script.Parent.Parent.Zone2Walls
local AlivePlrs = {}
print("check script starting")
game.Workspace.ChildAdded:connect(function(Char)
    if Char:IsA("Model") and Char:FindFirstChild("Humanoid") then -- Make sure it's a Character, not something else
        local Humanoid = Char.Humanoid
        AlivePlrs.insert(AlivePlrs, #AlivePlrs + 1, Char.Name)
        Humanoid.Died:connect(function() 
            for Position, Plr in ipairs(AlivePlrs) do
                if Plr == Char.Name then -- If they are in the table then...
                    AlivePlrs.remove(AlivePlrs, Position) -- Position is an iterator which gives a number, in this case we can use that number to find where their name is in this table and remove it
                end
            end
        end)
    end
end)
print("done with table")
wait(40)
function isEmpty(t)
    return next(t) ~= nil
end
if #AlivePlrs<=0 then
	print("ending game")
	script.Parent.TestScript.Disabled = true
	copy = script.Parent.TestScript:Clone()
	script.Parent.TestScript:Destroy()
        --ignore this area
	for _,Part in pairs(zone2Walls:GetDescendants()) do
		if Part:IsA("BasePart") then
			Part.Transparency = 1
		end
	end
	for _,Part in pairs(zone1Walls:GetDescendants()) do
		if Part:IsA("BasePart") then
			Part.Transparency = 1
		end
	end
       --ignore
end

Would there be an easier way to check if everyone playing the minigame had died?

1 Like

Usually you would have everything inside of a while true do for something like this, and in that given situation you could just do this

if #AlivePlrs == 0 then print("Round has ended") break end

It does end the game, but it ends it while the players are still alive somehow.

Before you remove the player from the table, do player:LoadCharacter()

It doesn’t make any noticable difference.
Here’s where I inserted it:

while true do
	local zone1Walls = script.Parent.Parent.Zone1Walls
local zone2Walls = script.Parent.Parent.Zone2Walls
local AlivePlrs = {}
print("check script starting")
game.Workspace.ChildAdded:connect(function(Char)
    if Char:IsA("Model") and Char:FindFirstChild("Humanoid") then -- Make sure it's a Character, not something else
        local Humanoid = Char.Humanoid
        AlivePlrs.insert(AlivePlrs, #AlivePlrs + 1, Char.Name)
        Humanoid.Died:connect(function() 
            for Position, Plr in ipairs(AlivePlrs) do
                if Plr == Char.Name then -- If they are in the table then...
--right
					Humanoid:LoadCharacter()
--here
                    AlivePlrs.remove(AlivePlrs, Position) -- Position is an iterator which gives a number, in this case we can use that number to find where their name is in this table and remove it
                end
            end
        end)
    end
end)
print("done with table")
wait(40)
if #AlivePlrs == 0 then
	print("ending game")
	script.Parent.TestScript.Disabled = true
	script.Parent.Parent.PacerTest:Stop()
	copy = script.Parent.TestScript:Clone()
	script.Parent.TestScript:Destroy()
	for _,Part in pairs(zone2Walls:GetDescendants()) do
		if Part:IsA("BasePart") then
			Part.Transparency = 1
		end
	end
	for _,Part in pairs(zone1Walls:GetDescendants()) do
		if Part:IsA("BasePart") then
			Part.Transparency = 1
		end
	end
	break end
end

Player was an unknown global, so I changed it to Humanoid instead.

Ok just do this instead for when you add the player to the table not the players name

for i, v in pairs(game.Players:GetPlayers()) do
		if v then
			table.insert(plrs,v)
		end
	end

and when added to the game, insert some sort of gametag into their character on game starting.


for i, v in pairs(plrs) do
    if v.Character then
        local Alive = Instance.new("BoolValue",v.Character)
        Alive.Name = "Alive"
    end
end

to check when the player is alive

for i, v in pairs(plrs) do
    if v.Character:FindFirstChild("Alive") == nil then
        table.remove(plrs,i)
   end
end

Also LoadCharacter is part of Player, not humanoid so that wouldn’t work

I’ve created this script: the game still ends abruptly when no players have died.

while true do
local zone1Walls = script.Parent.Parent.Zone1Walls
local zone2Walls = script.Parent.Parent.Zone2Walls
local  = {}
print("check script starting")
game.Workspace.ChildAdded:connect(function(plrs)
    for i, v in pairs(game.Players:GetPlayers()) do
		if v then
			table.insert(plrs,v)
		end
	end
	for i, v in pairs(plrs) do
	    if v.Character then
	        local Alive = Instance.new("BoolValue",v.Character)
	        Alive.Name = "Alive"
	    end
	end
	for i, v in pairs(plrs) do
	    if v.Character:FindFirstChild("Alive") == nil then
	        table.remove(plrs,i)
	   end
	end
end)
print("done with table")
if #plrs == 0 then
	print("ending game")
	script.Parent.TestScript.Disabled = true
	script.Parent.Parent.PacerTest:Stop()
	copy = script.Parent.TestScript:Clone()
	script.Parent.TestScript:Destroy()
	for _,Part in pairs(zone2Walls:GetDescendants()) do
		if Part:IsA("BasePart") then
			Part.Transparency = 1
		end
	end
	for _,Part in pairs(zone1Walls:GetDescendants()) do
		if Part:IsA("BasePart") then
			Part.Transparency = 1
		end
	end
	break end
	wait(1)
end

It’s basically just skipping past the first section of the code and doing the if #plrs == 0 statement anyways