Trying to remove a player from the table when they leave

When my round starts, it inserts all the current players into an alive table, but when a player leaves it ends the whole round even if there’s other players alive. How do I fix this?

local alivePlrs = {}

for i, plr in pairs(game.Players:GetPlayers()) do
	game.Workspace.Sounds.freshmeat:Play()
	--plr:LoadCharacter()
	plr.Character.HumanoidRootPart.CFrame = CFrame.new(-81.771, 20.852, 347.791)
	
	table.insert(alivePlrs, plr)
	
	plr.Character.Humanoid.Died:Connect(function()
		table.remove(alivePlrs, alivePlrs[plr])
	end)
	
	game.Players.PlayerRemoving:Connect(function()
	  print(plr.Name)
	  table.remove(alivePlrs, table.find(alivePlrs,plr.Name))
	end)
	
	if game.MarketplaceService:UserOwnsGamePassAsync(plr.UserId,18500950) then
		gunfolder.Minigun:Clone().Parent = plr.Backpack
	else
		if game.MarketplaceService:UserOwnsGamePassAsync(plr.UserId,18500955) then
			gunfolder.Bazooka:Clone().Parent = plr.Backpack
		else
			gunfolder.Glock:Clone().Parent = plr.Backpack
		end
	end
end

Try to do an if statement to make sure if the player is inside the alivePlrs table.

I literally can’t find any cuplrit inside this code.

I can see that you’ve got your PlayerRemoving event inside the for loop. This would repeat the event once per player in the game, and as you haven’t provided the plr parameter in the function, it’s going to remove every player from the table. I’d recommend moving this outside of the for loop.

local alivePlrs = {}

for i, plr in pairs(game.Players:GetPlayers()) do
game.Workspace.Sounds.freshmeat:Play()
--plr:LoadCharacter()
plr.Character.HumanoidRootPart.CFrame = CFrame.new(-81.771, 20.852, 347.791)

table.insert(alivePlrs, plr)

plr.Character.Humanoid.Died:Connect(function()
    table.remove(alivePlrs, alivePlrs[plr])
end)

if game.MarketplaceService:UserOwnsGamePassAsync(plr.UserId,18500950) then
    gunfolder.Minigun:Clone().Parent = plr.Backpack
else
    if game.MarketplaceService:UserOwnsGamePassAsync(plr.UserId,18500955) then
        gunfolder.Bazooka:Clone().Parent = plr.Backpack
    else
        gunfolder.Glock:Clone().Parent = plr.Backpack
    end
end
end

game.Players.PlayerRemoving:Connect(function(plr)
print(plr.Name)
table.remove(alivePlrs, alivePlrs[plr])
end)

Edit: Sorry, the text editor on Devforum is trash and butchered the spacing.

Oh thank you! I will try that out later when I get the chance