Remove a player from a table when he leaves the game

the script stops working whenever a alive player leaves the game during a round

so how do i remove them from the table when they leave the game?

while true do
	workspace.MobsFolder:ClearAllChildren()
	
	if workspace:FindFirstChild("Map") then
		workspace.Map:Destroy()
	end
	
	for i,v in pairs(game.Players:GetPlayers()) do
		v:LoadCharacter()
		local playerspawns = lobby:WaitForChild("PlayerSpawns"):GetChildren()
		local randomnum = math.random(1, #playerspawns)
		local chosenspawn = playerspawns[randomnum]
		v.Character.HumanoidRootPart.CFrame = chosenspawn.CFrame
	end
	
	statusValue.Value = "Intermission"
	
	lighting.FogEnd = 50
	lighting.FogStart = 10
	
	wait(30)
	
	lighting.FogEnd = 500
	lighting.FogStart = 0
	
	local randommapnum = math.random(1, #maps:GetChildren())
	local mapchildren = maps:GetChildren()
	local chosenmap = mapchildren[randommapnum]:Clone()
	local playerspawns = chosenmap:WaitForChild("PlayerSpawns"):GetChildren()
	local playerspawnnum = math.random(1, #playerspawns)
	local mobSpawns = chosenmap:WaitForChild("MobSpawns"):GetChildren()
	local mobspawnnum = math.random(1, #mobSpawns)
	chosenmap.Parent = workspace
	chosenmap.Name = "Map"
	
	
	local alivePlrs = {}
	
	for i, plr in pairs(game.Players:GetPlayers()) do
		
		plr:LoadCharacter()
		local chosenspawn = playerspawns[playerspawnnum]
		
		plr.Character.HumanoidRootPart.CFrame = chosenspawn.CFrame + Vector3.new(0,15,0)
		
		table.insert(alivePlrs, plr)
		
		
		
		plr.Character.Humanoid.Died:Connect(function()
			table.remove(alivePlrs, alivePlrs[plr])
		end)
		
		sword:Clone().Parent = plr.Backpack
	end
	
	
	for i = 1, maxWave do
		
		local mobsToSpawn = (startingMobs / 2) * (2 ^ i)
		
		
		statusValue.Value = "Wave " .. i .. " Comes."
		wait(3)

		local mobTypeSpawn
		for waveNum, mobType in pairs(mobs) do
			
			if i >= waveNum then
				
				mobTypeSpawn = mobType
				break
			end
		end
		
		local mobSpawns = chosenmap:WaitForChild("MobSpawns"):GetChildren()
		
		while mobsToSpawn > 0 do
			
		--	for x, spawner in pairs(mobSpawns) do
				
			mobsToSpawn = mobsToSpawn - 1
			local mobspawnnum = math.random(1, #mobSpawns)
			local spawner = mobSpawns[mobspawnnum]
			local mobClone = mobTypeSpawn:Clone()
			mobClone.Parent = workspace.MobsFolder
			mobClone.HumanoidRootPart.CFrame = spawner.CFrame + Vector3.new(0, 10, 0)
			mobClone.Name = "Mob"
			wait(1)
			
			if #alivePlrs < 1 then
				break
			end
			
		--	end
		end
		
		repeat
			wait()
			
			statusValue.Value = "Wave " .. i .. " | " .. #workspace:WaitForChild("MobsFolder"):GetChildren() .. "/" .. (startingMobs / 2) * (2 ^ i) .. " Enemies"
			
		until #workspace:WaitForChild("MobsFolder"):GetChildren() < 1 or #alivePlrs < 1
		
		if #alivePlrs < 1 then 
			break
			
		else
			
			for y, plrAlive in pairs(alivePlrs) do
				
				plrAlive.leaderstats.Gold.Value = plrAlive.leaderstats.Gold.Value + (20 * i)
			end
			
			if i % respawnWave == 0 then
				
				for z, plr in pairs(game.Players:GetPlayers()) do
					plr:LoadCharacter()
					sword:Clone().Parent = plr.Backpack
				end
			end
		end
	end
end

You can connect the PlayerRemoving event of the Players service to a function that’ll remove that player from the table.

Example:

local Players = game:GetService("Players")

Players.PlayerRemoving:Connect(function(playerWhoLeftTheGame)

    local playerCheck = table.find(alivePlrs, playerWhoLeftTheGame)
--[[ This will look through the table called "alivePlrs" to see if the player
who left the game was still listed in the table. I'm basing the name of the
table & the stored value on how you've done this in the codeblock provided --]]

    if playerCheck then
        table.remove(alivePlrs, playerCheck)

    --[[ If the player was still listed in the table, then
        that entry will be removed from the table. --]]

    end
end)
2 Likes
1 Like